[Hexadecimal - a relict from the past for 'modern' programmers?]
By far not. Even if programming languages like VB encapsulate more and more of the
underlying structure so that a programmer can quickly build programs without a steep
learning curve, it's absolutely necessary to know the basics in order to understand
issues when they occur.
Not to talk about the necessarity of understanding hex when it comes to reading
a 'dump'.
One of these basics is the HEX system. Where are hex values used?
Examples in web programming are: colors in CSS are represented through 3 hex values
(RGB), e.g. #c1c1c1 (a gray value), unless you use named colors.
Or when it comes to the topic: encoding of special characters, e.g. German
'Umlaute', in XML. Problems like misinterpretation of characters on client-side
can occur and in order to better understand what really happened it's useful to
understand hex. You have e.g. to decide for an encoding scheme and when dealing
with Unicode and XML one certainly gets in contact with decimal or hex representations
of the character value one wants to display.
What is hexadecimal (hex)?
The decimal system is based on the value '10', the hexadecimal system is based on
'16'.
A brief recap: 1 byte is represented by 8 bits, meaning 4 for each half-byte (0000
0000).
A half-byte can reflect the decimal values 0 to 15; the hex system knows the values
as 0 - 9 and A - F.
See following table:
hex binary dec
0 0000 0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
A 1010 10
B 1011 11
C 1100 12
D 1101 13
E 1110 14
F 1111 15
Altogether 16 different values. '
F' is the highest hex value 4 bits can represent, i.e. binary 1111 is
in hex: F.
Therefore 2 hex values are needed to represent 1 byte, e.g. hex FF (= 8 bits:1111
1111).
Different programming languages have a different syntax and prefixes to indicate
a hex value:
In XML, to represent Unicode chars, the prefix is: &#x (e.g. ® is a
'Registered Trademark sign' and ☺ is a Smiley).
In CSS colors are prefixed with: # (e.g. #c1c1c1 is a gray)
For a Registered Trademark sign in a XML file you would write either ®
(hex)or ® (decimal)
Binary
Following you see the lowest and highest value 1 byte (8 bit) can have.
| ............ 1 byte ...........|
| half-byte | | half-byte |
----------------------------------
128 - 64 - 32 - 16 8 - 4 - 2 - 1 value represented by each bit (generic expr.: 2
n)
----------------------------------
0 0 0 0 0 0 0 0 results in: dec 0 (= hex '00')
1 1 1 1 1 1 1 1 results in: dec 255 (=hex 'FF')
Conversion from binary to decimal works by multiplying the bit value (0 or 1)
with the value
represented by the position of the bit (128+64+32+16+8+4+2+1 = 255)
Hex
In hex each position represents a half-byte, the highest value per byte is: hex
FF (255).
Following some hex examples for 8bit (1-byte) and 16bit (2-byte) values: xFF, xAE, x111A
and how to convert a hex value to a decimal value:
| byte | |byte|
4096 - 256 - 16 - 1 value represented by each position (generic expr.: 16n)
-------------------
F F results in decimal 255 ((1 x 15)=15 plus (16 x 15)=240)
A E results in decimal 174
1 1 1 A results in decimal 4378 (1 x 4096 plus 1 x 256 plus 1 x 16 plus 10 x 1
Following the 16n values for 1bye, 2 bytes, 3 bytes, 4 bytes
You can do the math by following above samples. Or just type the hex values into e.g. your Windows Calculator which converts in all directions very nicely.
| 32bit | 24bit | 16bit | 8bit |
| byte 4 | byte 3 | byte 2 | byte 1 |
| FF | FF | FF | FF |
| 268,435,456 | 16,777,216 | 1,048,576 | 65,536 | 4,096 | 256 | 16 | 1 |
Max. values for 16bit or 32bit
In the programmer's world 16bit code, then 32bit code and now 64bit code is quite normal.
Basically the compiler does everything but the programmer must understand the maximum address space which can be reached.
8bit (1 byte) = hex FF = decimal 255 (a range from 0 to 255, altogether 256 different values possible)
16bit (2 bytes) = hex FF FF = decimal 65,535 (that's why you may encounter the term 64k as a limit so often, especially if the variable
which holds the value is a 16 bit unsigned integer.
A signed integer reserves one of the bits to store the sign (+/-) and therefore the value range is only from −32,768 to +32,767 (signed).
32bit (4 bytes) = hex FF FF FF FF = decimal 4,294,967,295 (~ 4Gig). That's why your OS can only address 4GB Memory in your 32bit machine, even if you installed 8GB.
Cheers, best regards,
Frank; rev. 2009-Aug