hex : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

hex hex
or hexadecimal, base 16 numbers, e.g. 08Cf, made of the digits 0..9 and a..f. This is the number system we would have used had we sixteen fingers instead of ten.
Even though in Java we specify decimal, hex, octal and other radix representations of int literals, inside the int, there is no radix. It is just string of 32 binary bits. When you display an int you are in some way picking a radix and converting it to String. Methods such an println do this for you, so it creates the illusion you are seeing the value of the int directly. It does not make sense to talk of the radix of an int, e.g. is this a hex or decimal int?, only the radix of some String representation of it.
Displaying With Hex Strings Hex Literals
Hex with Lead Zeroes Hex vs Decimal vs Binary
Hex String to int Table of Hex digits
String to hex String Hex Color Numbers
byte[] to String Links
String to byte[]

Displaying With Hex Strings

You

Hex with Lead Zeroes

That won’t apply any lead zeroes. Here is how to get a lead zero for a fixed two character hex representation

The method com.mindprod.common18.ST. toHexString is useful for debugging. It will give you an 8-char left-zero padded hex String representation of an

// Available in download http://mindprod.com/products1.html#COMMON11
import com.mindprod.common11.StringTools;
...
// Prepare unsigned hex string padded out to 8 chars with lead zeroes.
final String display = StringTools.toHexString( someInt );

The method com.mindprod.common18.ST. toLzHexString will pad out

Hex String to int

You can convert a hex String to internal binary like this:
/* convert a hex String to int */
// Note, there is no lead 0x, case-insensitive
String g = "af0c99";
int i = Integer.parseInt( g.trim(), 16 /* radix */ );

Since the computer uses binary internally, it makes no sense to talk about converting an int from hex to decimal or back, only a String.

String to hex String

Here is how you can convert a String to hex for display so you can tell precisely what each

Use can also use Printwriter.printf ( %x, i ) to display a hex number.

byte[] to String

The following code looks complicated but it is fast. Joerg Meier noted that is very similar to the code for javax.xml.bind.DatatypeConverterImpl.printHexBinary that comes with the JRE (Java Runtime Environment). It also handles a byte array, not just a single byte.

String to byte[]

Here is a low-level routine to parse a hex string to byte array.

Hex Literals

Hex literal are written with a lead 0x. byte literals are usually padded out to 2 digits, e.g. 0x0f. short and char literals are usually padded out to 4 digits, e.g. 0x00ff. int literals are sometimes padded out to 8 digits, e.g. 0x000022ff. long literals are sometimes padded out to 16 digits, e.g. 0x0000000010000022ff. Unfortunately, Java does not allow commas to break long literals into groups of four digits. You can insert spaces to proofread then remove them to compile. Ideally editors would work like JDisplay, showing groups in slightly different alternating colours as in the following example.

// example of a hex 0x literal broken into groups by of four
// by colour for easy proofreading.

int a = 0x00aceb777708cf;

Hex vs Decimal vs Binary

The computer chip works internally in binary (base 2 numbers), with numbers made only of the digits 0 and 1, low voltage/high voltage. Binary numbers are somewhat bulky to write out, so they are usually written in base 16, hex. It is very easy to convert hex to binary and visa versa using the following table. For example, the hex number 8cf is 100011001111 in binary. Converting between decimal and hex is more difficult. You must do successive divisions and moduluses by 10 or 16.

Table of Hex digits

Decimal Hex Binary
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
10 a 1010
11 b 1011
12 c 1100
13 d 1101
14 e 1110
15 f 1111
You can handle arbitrarily large hex Strings with BigInteger.

Hex Color Numbers

Learning More

Oracle’s Javadoc on Integer.toHexString : available:
Oracle’s Javadoc on Long.toHexString : available:
BigInteger
binary
binary formats
BitSet
conversion
HexView: allows you to view files in hex
Learn To Count Applet to sharpen your intuition on how binary, hex and octal work
literals
masking
mixed base
octal
PGP hex encoding as words
powers of two table
printf
radix
shift
SlickEdit: allows you to view files in hex
Sweet 010 Binary Editor
unsigned
Wotsit

This page is posted
on the web at:

http://mindprod.com/jgloss/hex.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\hex.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[44.222.149.13]
You are visitor number