Java – Hexadecimal

Hexadecimal system is 16 based, digit can be used is from range 0 – 9 A – F.

Prefix – 0x

Hexadecimal literal have a prefix 0x. Example:

public static void main(String[] args) {
int first = 0xA; #10
int second = 0xB; #11
int third = 0xC; #12
}

Print Decimal Value

Since the digit start from 0-9A-F, meaning the 10=A, 11=B, 12=C, 13=D, 14=E, 15=F

public static void main(String[] args) {
 int first = 0xA;  
 int second = 0xB; 
 int third = 0xC;  
  
 System.out.println("first " + first);   //10
 System.out.println("second " + second); //11 
 System.out.println("third " + third);   //12
}

What is the hex value 16?

String hex = Integer.toHexString(16); // 0x10

/* verify the answer
* (1 * 16^ 1) + (0 * 16^0 )
 * (1 * 16) + ( 0 * 1)
 * 16 + 0

Example 1

public static void main(String[] args) {
int first = 0x14A;
System.out.println("first " + first); // 330
}

Calculation

> 1 4 A
> (1 * 16^2) + (4 * 16^1) + (10 * 16^0)
> 256 + 64 + 10
> 330

Java – Hexadecimal

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.