# How to Convert Decimal to Hexadecimal in Java

**How to Convert Decimal to Hexadecimal in Java**
The hexadecimal number uses **16** values to represent a number. Numbers from **0** to **9** represented by **digits** and the numbers from **10** to **15** are represented by **alphabets** from **A** to **F.**
There are **two** methods to convert Hexadecimal to decimal are as follows:

- Using
**toHexString() method** - Using
**user-defined logic**

**Using Integer.toHexString() method**The

**toHexString()**is a

**static**method of

**Integer wrapper class.**It accepts a

**single parameter**of

**integer**type and returns a

**string**representation of

**int**argument in radix

**16**. The signature of the method is given below: public static String toHexString(int decimal_number) Where

**decimal_number**is the number which you want to convert.

**Example**In the following example, we have taken two variables

**str1**and

**str2**of type

**String**which stores the converted

**Hexadecimal**value. The

**toHexString()**is the method of Integer wrapper class which parses the integer arguments

**34**and

**75**respectively. The first

**println**statement prints the hexadecimal representation of

**34**and the second

**println**statement prints the hexadecimal representation of

**75**.

public class DecimalToHexaExample { public static void main(String args[]) { String str1=Integer.toHexString(34); String str2=Integer.toHexString(75); System.out.println(str1); System.out.println(str2); } }

**Output**

22 4b

**Using user-defined logic**You can convert decimal to hexadecimal in using user-defined logic.

**Example**In the following example, we have taken

**two**variables

**decnum**(represent

**decimal**number) and

**rem**(represent

**remainder**). We have initialized

**45**to

**decnum**. A

**String**type variable

**hexdecnum**initialized to

**null**and it stores the

**convertedhexadecimal**value.We have defined an array

**hex[]**of type

**char**and initialized

**hexadecimal**valuesfrom

**0**to

**F**to it. The

**while**loop has the condition

**decnnum>0**which will repeatedly perform the operations in the while block until

**decnum**is

**greater than 0**otherwise terminate the execution.The body of the while loop contains

**three**statements. The

**first**statement finds the remainder on dividing

**decnum**by

**16**, which is obtained by using a modulo operator. In the

**second**statement, the

**hex[rem]**converts the

**remainder (rem)**into

**character**equivalent and concatenates the

**remainder(rem)**into

**hexdecnum,**and stores the

**results**into the variable

**hexdecnum**. The

**third**statement

**updates**the value of

**decnum**, i.e., it divides the value by

**16**and takes the

**quotient**. These steps perform the operation in the while block until the condition

**decnum>0**becomes

**false**. When the execution of the while loop is completed the

**println**statement prints the

**hexadecimal**representation of the variable

**decnum**which is stores in

**hexdecnum**. It prints the array in

**reverse**order.

**Remember:**If you write the

**second**statement of while loop like this:

**hexdecnum=hex[rem]+hexdecnum**then the output will be

**2D.**When you change the sequence of the

**second**s tatement i.e.

**hexdecnum=hexdecnum+hex[rem]**then the hexadecimal representation of

**45**will be

**D2**which is the wrong output.So remember the sequence while you write this statement. This statement performs

**concatenation**instead of

**addition**. One more thing to remember, if you assign any digitamong

**0**to

**9**it gives the same

**digit**as output but in

**string**form.

public class DecimalToHexaExample1 { public static void main(String args[]) { intdecnum=45, rem; String hexdecnum=""; char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; while(decnum>0) { rem = decnum%16; hexdecnum = hex[rem] + hexdecnum; decnum = decnum/16; } System.out.println("Hexadecimal Value of 45 is:"+hexdecnum); } }

**Output**

Equivalent Hexadecimal Value of 0 is: 2D