In the last post we saw how to convert decimal number into binary number in java but here we will learn how to convert decimal to hexadecimal in java.
Let's start java program to convert decimal to hexadecimal.
Convert Decimal to hexadecimal in java
We can convert decimal number to hexadecimal number in java by two ways and these are given below.
- By using toHexString() method of Integer class which is wrapper class.
- By writing your own logic, without using any predefined method.
(1) Let's write a java program to convert a decimal number to hexadecimal number using "toHexString()" method of Integer class.
class DecimalToHexadecimal
{
public static void main(String args[])
{
int i = 32;
String str = Integer.toHexString(i);
System.out.println("After conversion hexadecimal number is: "+str);
}
}
Output: After conversion hexadecimal number is 20
Or
This is the same example as above but here we will use Scanner class so that we can take input from the users.
import java.util.*;
class Simple
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int number = input.nextInt();
//using toHexString() method of Integer class
String str = Integer.toHexString(number);
System.out.println("Hexadecimal number is: "+str);
}
}
Output: Enter a decimal number:
45
Hexadecimal number is: 2d
(2) Program to convert decimal to hexadecimal in java without using any ready made method. Here we will put own logic for conversion.
import java.util.Scanner.*;
class Simple
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
//taking input from the user
System.out.println("Enter decimal number: ");
int number = input.nextInt();
int rem;//for storing remainder
String str = "";//for storing result
char hexa[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(number>0)
{
rem = number%16;
str = hexa[rem]+str;
number = number/16;
}
System.out.println("Hexadecimal Number is: "+str);
}
}
Output: Enter decimal number:
45
Hexadecimal Number is: 2D
class DecimalToHexadecimal
{
public static void main(String args[])
{
int i = 32;
String str = Integer.toHexString(i);
System.out.println("After conversion hexadecimal number is: "+str);
}
}
Output: After conversion hexadecimal number is 20
Or
This is the same example as above but here we will use Scanner class so that we can take input from the users.
import java.util.*;
class Simple
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int number = input.nextInt();
//using toHexString() method of Integer class
String str = Integer.toHexString(number);
System.out.println("Hexadecimal number is: "+str);
}
}
Output: Enter a decimal number:
45
Hexadecimal number is: 2d
(2) Program to convert decimal to hexadecimal in java without using any ready made method. Here we will put own logic for conversion.
import java.util.Scanner.*;
class Simple
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
//taking input from the user
System.out.println("Enter decimal number: ");
int number = input.nextInt();
int rem;//for storing remainder
String str = "";//for storing result
char hexa[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(number>0)
{
rem = number%16;
str = hexa[rem]+str;
number = number/16;
}
System.out.println("Hexadecimal Number is: "+str);
}
}
Output: Enter decimal number:
45
Hexadecimal Number is: 2D
How to convert Hexadecimal to decimal number in java?
This is the simple example where we are going to convert hexadecimal number to Decimal number in java. In this example we will use parseInt() method of Integer wrapper class.
class HexaToDecimal
{
public static void main(String args[])
{
//String containing hexadecimal number
String hexanumber = "20";
//we will use parsInt() method of Integer wrapper class
int decimalnumber = Integer.parseInt(hexanumber, 16);//pass 16
System.out.println("hexadecimal number will be convert into decimal");
System.out.println("Decimal number is: "+decimalnumber);
}
}
Output: hexadecimal number will be convert into decimal
Decimal number is: 32
Above all the examples are quite simple and easy and it is very useful examples. There are many java number conversion interview questions are asked in java or core java interviews e.g....
how to convert decimal to binary number with example
how to convert decimal to hexadecimal number with example.
how to convert decimal to octal number in java with example.
how to convert binary to decimal with example.
how to convert binary to hexadecimal.
how to convert binary to octal with example.
how to convert hexadecimal to binary, decimal, octal.
and many more...but above we saw simple examples of how to convert decimal to hexadecimal and conversion of hexadecimal to decimal number in java.
Output: hexadecimal number will be convert into decimal
Decimal number is: 32
Above all the examples are quite simple and easy and it is very useful examples. There are many java number conversion interview questions are asked in java or core java interviews e.g....
how to convert decimal to binary number with example
how to convert decimal to hexadecimal number with example.
how to convert decimal to octal number in java with example.
how to convert binary to decimal with example.
how to convert binary to hexadecimal.
how to convert binary to octal with example.
how to convert hexadecimal to binary, decimal, octal.
and many more...but above we saw simple examples of how to convert decimal to hexadecimal and conversion of hexadecimal to decimal number in java.
Great job. Your all java blog posts are awesome. Keep it up.
ReplyDeleteThanks for sharing this informative blog java training in OMR
ReplyDelete