Had we only 8 fingers we might have used base 8, (aka octal radix 8). In octal you count 0 1 2 3 4 5 6 7 10 11 12…
Computers internally always use base 2 (aka binary, radix 2) in which you count like this 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111…
People intererested in the internals of computers like to use base 16 (aka hex, hexadecimal, radix 16). In it you count like this:0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20… It is very easy to interconvert hex back and forth to binary using this little table:
| Binary to Hex Conversion | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| binary | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 |
| hex | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
| decimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
You can convert the computers internal binary formats (int, long) to Strings in any radix you want and back using built-in Java conversion functions.
Java has special support for octal and hex literals, and strings in number bases 2 through 36.
// convert int to String with given radix String g = Integer.toString( i, 36 /* radix 2 to 36 */ ); // convert String to int when string is encoded with a given radix int i = Integer.parseInt( g.trim(), 36 /* radix 2 to 36 */ );For larger radixes, you could examine Sun’s code and write your own code with a larger cast of encoding characters.
|
|
available on the web at: |
http://mindprod.com/jgloss/radix.html |
optional Replicator mirror
|
J:\mindprod\jgloss\radix.html | |
![]() |
Please email your
feedback for publication,
letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear
wording, broken/redirected link reports, suggestions to improve this page or comments to
Roedy Green :
| |
| Blog | Canadian
Mind
Products
IP:[65.110.21.43] Your face IP:[23.22.76.170] |
|
| Feedback | You are visitor number 45,709. | |