Java Program to illustrate the Usage of HexaDecimal
Last Updated :
30 Jul, 2024
A decimal number is the sum of digits multiplied with a power of 10. Decimal numbers are represented with base 10 whereas hexadecimal Number is the sum of digits multiplied with the power of 16, somehow there are similar traits in the internal working of them just they are two different ways to present a number. The hexadecimal system has 16 different digit symbols. Different numbers can be generated using the combination of digits from 0 to 15. The representation in the hexadecimal system is the same as the decimal number system from 0 to 9 but then onwards it changes.
Representation:
Decimal Number Hexadecimal Number Equivalent
0 ---> 0
: :
9 ---> 9
10 ---> A
11 ---> B
12 ---> C
13 ---> D
14 ---> E
15 ---> F
Illustration: Internal Working
A. Decimal to Hexadecimal Number System
(1) (13)10 --> (D)16
Directly can be written 13 as D in hexadecimal system
(2) (16)10 ---> (10)16
( 16 )16 = ( 1 x 161) + ( 0 * 160)
(3) (59)10 ---> (3B)16
( 59 )10 --> ( 3 * 161) + (11 * 160)
Conclusion:
For a Decimal Number system-> ( 421 )10 = (4 x 102) + (2 x 101) + (1 x 100)
B. Similarly ,Hexadecimal to Decimal Number System
(8A)16 ---> (138)10
(8A)16 --> (8 x 161) + (10 x 160)
Conclusion:
In Java programs, hexadecimal numbers are written by placing 0x before numbers.
Below are 4 examples been discusses to illustrate the usage of Hexadecimal Number
- Converting Hex number to Decimal number
- Converting Decimal number to Hex number
- Converting Hex number to Long number
- Converting Long number to Hex number
Example 1: Java program to convert Hex number to Decimal number
Java
// Java program to convert Hex number to Decimal number
// Importing input/output java library
import java.io.*;
class GFG {
// Main driver method
public static void main(String[] args)
{
// Hexadecimal number stored in a string
String hexNum = "100";
/* Random hexadecimal number */
// Passing hexnum and base as parameters
// which is 16 to parseInt function
int decimal = Integer.parseInt(hexNum, 16);
// Printing the output result as
// decimal equivalent of hexa-decimal
System.out.println("Decimal value is " + decimal);
}
}
OutputDecimal value is 256
Example 2: Java program to convert Decimal number to Hex number
Java
// Java Program to Illustrate the Usage of HexaDecimal
// Importing input/output java library
import java.io.*;
class GFG {
// Main driver function
public static void main(String[] args)
{
/* Decimal number to be converted */
int i = 257;
// Using toHexString() method for getting decNum and
// Storing the hexaDecNum in a string
String hex = Integer.toHexString(i);
// Printing hexaDecNum of decNum
System.out.println("Hex value is " + hex);
}
}
Example 3: Java program to convert Hex number to Long number
Java
// Java Program to Illustrate the Usage of HexaDecimal
// Importing input/output java library
import java.io.*;
class GFG {
// Main driver method
public static void main(String[] args)
{
// Hexadecimal number stored in a string
String hexNum = "10000";
// passing hexnum and base as parameters
// which is 16 to parseLong function
long num = Long.parseLong(hexNum, 16);
// Printing long value of HexaDecNum
System.out.println("Long value is " + num);
}
}
OutputLong value is 65536
Example 4: Java program to convert Long number to Hex number
Java
// Java Program to Illustrate the Usage of HexaDecimal
// Importing java input/output library
import java.io.*;
class GFG {
// Main driver function
public static void main(String[] args)
{
/* Long number to be converted */
long i = 1024;
// Storing the result in a string
String hex = Long.toHexString(i);
// Displaying Result
System.out.println("Hex value is " + hex);
}
}
Note: There are two more conventions for hexadecimal numbers, 400h or $400. They both are the same as 0x400.
Similar Reads
Java Program to Illustrate Use of Binary Literals A binary literal is a number represented in binary (0s and 1s). In Java, you can use binary literals for integral types such as byte, short, int, and long. To specify a binary literal, prefix the number with 0b or 0B. This allows you to define numbers in binary format directly in your code. When exe
4 min read
Java Program to Print the ASCII Value ASCII is an acronym that stands for American Standard Code for Information Interchange. In this, a specific numerical value is assigned to different characters and symbols, it is a 7-bit character set containing 128 (0-127), for computers to store and manipulate, and while storing and manipulating t
5 min read
Java Program to Convert Binary to Hexadecimal The Hexadecimal number system as the name suggests comprises 16 entities. These 16 entities consist of 10 digits, 0-9 representing the first 10 numbers of the hexadecimal system as well. For the remaining 6 numbers, we use English alphabets ranging from A through F to represent the numbers 10 to 15.
6 min read
Program to Convert Octal to Hexadecimal Given an Octal number, the task is to convert it into a Hexadecimal number. Examples: Input: 47 Output: 27Explanation:Decimal value of 47 is = (7 * 1) + (4 * 8) = 39Now, convert this number to hexadecimal39/16 -> quotient = 2, remainder = 72/16 -> quotient = 0, remainder = 2So, the equivalent
15+ min read
Java Program to Generate Random Hexadecimal Bytes To generate Random Hexadecimal Bytes, first, a random byte can be generated in decimal form using Java.util.Random.nextInt() and then it can be converted to hexadecimal form using Integer.toHexString() method. 1. Java.util.Random.nextInt() The nextInt() method is used to obtain the next integer from
2 min read
Java Program to Convert Binary to Octal A Binary (base 2) number is given, and our task is to convert it into an Octal (base 8) number. There are different ways to convert binary to decimal, which we will discuss in this article.Example of Binary to Octal Conversion:Input : 100100Output: 44Input : 1100001Output : 141A Binary Number System
5 min read
Java Program to Convert Char to Int Given a char value, and our task is to convert it into an int value in Java. We can convert a Character to its equivalent Integer in different ways, which are covered in this article.Examples of Conversion from Char to Int:Input : ch = '3'Output : 3Input : ch = '9'Output : 9The char data type is a s
4 min read
Java Program to Convert Octal to Binary Given an Octal number as input, the task is to convert that number into its Binary equivalent number. Example: Input: Octal Number = 513 Output: Binary equivalent value is: 101001011 Explanation : Binary equivalent value of 5: 101 Binary equivalent value of 1: 001 Binary equivalent value of 3: 011Oc
5 min read
Java Program For Decimal to Hexadecimal Conversion Given a decimal number N, convert N into an equivalent hexadecimal number i.e. convert the number with base value 10 to base value 16. The decimal number system uses 10 digits 0-9 and the Hexadecimal number system uses 0-9, A-F to represent any numeric value.Examples of Decimal to Hexadecimal Conver
3 min read
Java Program for Hexadecimal to Decimal Conversion Given a Hexadecimal (base 16) number N, and our task is to convert the given Hexadecimal number to a Decimal (base 10). The hexadecimal number uses the alpha-numeric values 0-9 and A- F. And the decimal number uses the numeric values 0-9. Example: Input : 1ABOutput: 427Input : 1AOutput: 26Approach t
3 min read