Hex vs binary vs ASCII vs Decimal
I have a general question about Hex values vs other formats what is the advantage to representing a number or other value in hex as opposed to other formats? Does it speed up the process? I am about trying to figure out the best way to name items nodes etc and I am wondering if there is any advantage to use hex vs #1 or One etc.
Comments
Converting a number to its hex representation uses only shifts which is usually straightforward. You also have to do an extra decision (0-9 vs. A-F) and decide what to use to represent the extra hex values (usually A-F).
Converting a number to its octal or binary representation is straightforward. These take more characters to represent a given value than either decimal or hex.
You can also represent numbers using a 5 bit code where you use 0-9 to represent values from 0 to 9 and uppercase letters A-V to represent values from 10 to 31.
There are other codes you could make up that would use the digits, upper and lowercase letters, and two punctuation characters like "$" and "%" to represent a six bit value.
OUTA[0..8] := %01010000
Or you could waste some time and figure out what the decimal value would be...
OUTA[0..8] := 80
It is easier to "visualize" the port if you access it in Binary...
About hex...a single decimal digit can only have 10 different values(Including zero), but Hexadecimal can have 16 different values....That makes it easier to represent a larger number(With fewer digits)...
http://en.wikipedia.org/wiki/Hexadecimal
Use different number types for different reasons..
Settings for this and that might be in binary in the data sheet. And the programmer made the setting in hex. So to figure out what the setting does, you need to convert the hex number to binary, then you can see what it is setting / not setting when looking at the data sheet.
So I like to use the same number system as what is in the data sheet.
If the data sheet for the microcontroller has settings in hex, then I will use hex. Or if in binary, then I will use binary.
So for example the data sheet might say this for settings...
10101 - APIN=0
11010 - APIN=1
11100 - BPIN=1
What setting is hex 1A?
What setting is binary 11010?
The different representations can help make tasks understood. One other thing I like about hex, is a single digit is 4 bits, two is a byte, etc...
Often in computing, those powers of two are handy, sometimes necessary boundaries.
I recommend you work with Hex and Binary often, and learn the first 16 powers of 2, and the first 4 powers of 16.
That will seriously improve your ability to read other code, and divide the memory space into sensible pages. The sizes of things make a lot of sense too. Just looking at a hex number tells you a lot about the resource use of something, or the placement, where decimal tends to distract from that some.
By all means, they are not necessary, just very handy.