How do you convert to/from hexadecimal in Java?

This article explains hexadecimal numbers and then shows how you can convert a string to an integer in Java and vice versa in order to perform hex/decimal conversions. The links at the bottom of the page also provide further resources related to string and data conversion in Java.

Introduction to hex

Hexadecimal (or "hex" for short) is a numbering system which works similarly to our regular decimal system, but where a single digit can take a value of 0-15 rather than 0-9. The extra 'digits' are the letters A-F, which represent decimal values 10-15, as shown in the table below. In Java code (as in many programming languages), hexadecimal nubmers are written by placing 0x before them. For example, 0x100 means 'the hexadecimal number 100' (=256 in decimal).

DecimalHexadecimal
00
11
...
99
10A
11B
12C
13D
14E
15F
Decimal values of hexadecimal digits.

In the decimal system, since there are 10 possible digits, each digit that we add to the left of a number has ten times the place value. For example, in a three digit number, the three digits represent hundreds (=10x10), tens and units respectively. In the hexadecimal system, there are 16 possible digits. So each time we add a digit to th eleft, its place value is 16 times greater. So in a two-digit hex number, the first digit represents the number of 16s, and the second digit the number of units (where the highest unit has a decimal value of 15). For example, the hexadecimal number 0x8A represents a decimal value of (8 * 16 + 10) = (128 + 10) = 138. In a three-digit hexadecimal number, the first digit represents the number of 256s (=16*16), etc.

Why use hexadecimal?

You may be wondering why we would want to use the hexadecimal numbering system at all. Simply put, the reason is that hexadecimal is more "in tune" with how numbers are organised internally when it comes to computer memory and data. The divisions between hexadecimal digits correspond more closely to the divisions between the bytes making up that number when it is stored. The number 1024— the multiplier for going from byte to kilobyte to megabyte etc— becomes 400 in hex and so values such as 2048 (=2K) which look a little "awkward" or "hard to judge" in decimal become nice "round" numbers when converted into hex. Here are some examples:

Hex numberDecimal equivalentComm meaning/usage
100256Number of possible values that a single byte can hold.
4001024Number of bytes in a kilobyte; number of kilobytes in a megabyte, etc.
20008192Number of bytes in 8K; number of bytes in a SQL Server database page (and other common file/memory organisation).
1000065536Number of bytes in 64K; possible values that two bytes together can hold. (Or, for example, possible values that can fit in a Java short.)
100000016777216Number of distinct colours if we assign one byte each to the red, green and blue components. (Number of values that can be assigned to a 24-bit number.)

Notice how these numbers are slightly 'awkward' in decimal, but are nice "round" numbers in hexadecimal. For this reason, once you get used to working in hex, it can actually be more convenient for counting things such as memory offsets or number of combinations of values compared to working in decimal. Other common uses of hex that you will regularly see in code include colour definitions and UTF8 character codes.

How to convert from decimal to hex

Often, we will have a hex number in a string and need to parse or convert the number stored in the string. Assuming that we have our hex number in a string, then a simple way to convert the hex number to decimal is to call Integer.parseInt(), passing in 16 as the second parameter:

String hexNumber = ...
int decimal = Integer.parseInt(hexNumber, 16);
System.out.println("Hex value is " + decimal);

The number 16 refers to base 16, i.e. a number system (hexadecimal) where each digit represents one of 16 values (0-9 and A-F give 16 possibilities, representing the values 0-15 as in the table above).

In order to convert larger or unsigned numbers to decimal, we will need to use other methods such as Long.parseLong() (see also the example below).

How to convert from decimal to hex

To go the other way round and convert decimal to hex in Java, you can use the utility method Integer.toHexString(). If you have the value that you want to convert in an int variable, then you can simply call:

int i = ...
String hex = Integer.toHexString(i);
System.out.println("Hex value is " + hex);

If you have the decimal number in a String, then you first call Integer.parseInt() but this time you don't need any second parameter— decimal is the default:

String string = ...
int no = Integer.parseInt(string);
String hex = Integer.toHexString(no);
System.out.println("Hex value is " + hex);

How to convert a long to and from hex

In case you need it, the Long class has Long.parseLong() and Long.toHexString() analogous to the Integer methods above.

How to write a hex number in Java code

Sometimes it is convenient to represent a number in hex in your Java code. There are generally two conventions for writing hex numbers. The first is to write h after the hex number. For example, 400h is the equivalent of 1024 decimal. This convention is mainly used in written texts.

The second convention was already mentioned above. This is to put 0x before the number, e.g. 0x400. This is the convenion used by Java, as in many programming languages nowadays:

int noBytes = 0x400; // = 1024

(Another convention, sometimes used in assembly language, is to place a dollar sign ($) before the hex number, e.g. $400 is the same as 400h or 0x400.)

How to write 64-bit hex numers in Java code

As you are probably aware, if you want to write a long (64-bit) constant in Java code, you must place an L after the constant. The same is true if you write the number in hexadecimal. In this case, you will end up with the 0x prefix and the L suffix. For example:

long mask = 0xFF00FF00FF00FF00L;

Related articles

The following articles contain further information on topics related to the conversion of numbers and data in Java:

A variety of other Java tutorials are also available on this site.


If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.