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 radix16.  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.

Output

Using user-defined logic

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

Example

In the following example, we have taken two variablesdecnum (represent decimal number) and rem (represent remainder). We have initialized 45 todecnum. A String type variable hexdecnum initialized to null and it stores the convertedhexadecimal value.We have defined an array hex[]of type charand 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 decnumis 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 theremainder(rem) into hexdecnum,and stores the results into the variable hexdecnum.

The third statement updates the value ofdecnum, i.e., it divides the value by 16 and takes the quotient.

These steps perform the operation in the while block until the conditiondecnum>0 becomes false. When the execution of the while loop is completed the println statement prints the hexadecimalrepresentation 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 seconds 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 digitamong0 to 9it gives the same digit as output but in string form.

Output

Pin It on Pinterest

Share This