hex : Java Glossary
home H words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (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.
Displaying With Hex Strings
Hex Literals
Hex vs Decimal vs Binary
Table of Hex digits
Hex Color Numbers
Links

Displaying With Hex Strings

You can display in hex using code like this:
That won’t apply any lead zeroes. Here is how to get a lead 0 for a fixed two character hex representation of a byte:
The method com.mindprod.common11.StringTools. toLzHexString will apply the necessary lead zeros for you:
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 */ );

The following code looks more complicated, but is considerably faster. It also handles a byte array, not just a single byte.

And here is how to go the other way from a hex string back to a byte array:
Here’s an alternate implementation of charToNibble.
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.

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

Hex Literals

In Java you can use hex literals like this
// example of a hex 0x literal
int a = 0x8cf;

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

010 Editor: a binary editor with templates
BigInteger
binary
binary formats
BitSet
conversion
Hex Editors
HexView: allows you to view files in hex
Learn To Count Applet to sharpen your intuition on how binary, hex, and octol work
literals
masking
mixed base
octal
PGP hex encoding as words
radix
shift
SlickEdit: allows you to view files in hex

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.17] The information on this page is for non-military use only.
You are visitor number 145,397. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/hex.html J:\mindprod\jgloss\hex.html