# Java convert decimal to hex – Vice Versa

Let’s learn java convert decimal to hex.

## Java convert decimal to hex

Write a java program to convert a decimal number to hexadecimal number is one of the common java interview programs for programming.

Also read – decimal to binary in java

In number system, decimal number has a base of ten. It has digits from 0 to 9. Whereas hexadecimal number has a base 16.

It has digits from 0 to 9 and A to F. Now let’s see the comparison through a table,

As you can see in the above table, from 0 to 9 the numbers are same in both number system.

Also read – decimal to octal in java

After 9, 10 is denoted as ‘A’, 11 is denoted as ‘B’, 12 is denoted as ‘C’, 13 is denoted as ‘D’, 14 is denoted as ‘E’, 15 is denoted as ‘F’ in the hexadecimal system.

Before going to java program to convert decimal to hexadecimal first let’s see its algorithm.

### Algorithm

1. class Scanner is used to take user inputs by creating scanner object which is present in java.util package.
2. create int decimal variable.
3. using nextInt() method of Scanner class take number entered by user and assign it to decimal variable.
4. Then create string variable which stores converted hexadecimal number.
5. Now to convert decimal number to hexadecimal number iterate decimal value till it is not equal to 0 using while loop.
6. create integer variable ‘hexNumber’.
7. Divide decimal number by 16, store remainder in hexNumber variable.
8. create character variable ‘charHex’. This variable holds hexadecimal numbers which have characters A, B, C, D, E, and F.
9. now if variable ‘hexNumber’ value is less than 9 take hex value appended with 0 character and store in ‘charHex’ variable.
10. if hex value is greater than 9 then append character ‘A’ to hex value minus 10. Because after 9 we have A, B, C, D, E and F.
11. Assign ‘charHex’ value appended with hexadecimal and assign it back to hexadecimal variable.
12. For each iteration of while loop take previous hexadecimal value appended to ‘charHex’ and assign it back to hexadecimal.
13. Repeat while loop with new value of decimal number taking division of decimal by 16.

Here’s java program to convert decimal to hex.

```import java.util.Scanner;
public class DecimalToHexaExample
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int decimal = sc.nextInt();
while(decimal != 0)
{
int hexNumber = decimal % 16;
char charHex;
if(hexNumber <= 9 && hexNumber >= 0)
{
charHex = (char)(hexNumber + '0');
}
else
{
charHex = (char)(hexNumber - 10 + 'A');
}
decimal = decimal / 16;
}
sc.close();
}
}

```

Output:

Let’s learn two more ways in java decimal to hex conversion,

• using Integer.toHexString() method – this method returns string representation of the unsigned integer value represented by the argument in hexadecimal (base 16).
• without using predefined method.

Convert decimal number to hexadecimal number using Integer.toHexString() method.

```import java.util.Scanner;
public class DecimalToHexaExample
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter decimal number : ");
int decimalNumber = sc.nextInt();
String str = Integer.toHexString(decimalNumber);
System.out.println("The hexadecimal value is : " + str);
sc.close();
}
}

```

Output:

Please enter decimal number : 12
The hexadecimal value is : c

#### java convert decimal to hex – without using predefined method

Let’s see java decimal to hex conversion without using predefined method.

```import java.util.Scanner;
{
public static void main(String[] args)
{
int temp, decimalNumber;
char[] hexa = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
Scanner sc = new Scanner(System.in);
System.out.print("Please enter decimal number : ");
decimalNumber = sc.nextInt();
while(decimalNumber > 0)
{
temp = decimalNumber % 16;
decimalNumber = decimalNumber / 16;
}
sc.close();
}
}

```

Output:

Please enter decimal number : 10
The hexadecimal value is : A

java program to convert decimal to hexadecimal using recursion

Let’s learn java program convert decimal to hexadecimal using recursion.

```import java.util.Scanner;
{
char[] charHexa = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int num;
String strHex = "";
{
if(h != 0)
{
num = h % 16;
strHex = charHexa[num] + strHex;
h = h / 16;
}
return strHex;
}
public static void main(String[] args)
{
int decimal;
Scanner sc = new Scanner(System.in);
decimal = sc.nextInt();
System.out.println(hex);
sc.close();
}
}

```

Output:

convert string to hex java

Now let’s learn how to convert string to hex java.

```import java.util.Scanner;
public class StringToHexJava
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String strInput = sc.next();
StringBuffer sb = new StringBuffer();
char[] chArray = strInput.toCharArray();
for(int a = 0; a < chArray.length; a++)
{
}
String strOutput = sb.toString();
System.out.println(strOutput);
sc.close();
}
}

```

Output: