Shop OBEX P1 Docs P2 Docs Learn Events
Hex vs binary vs ASCII vs Decimal — Parallax Forums

Hex vs binary vs ASCII vs Decimal

172heavy172heavy Posts: 55
edited 2010-10-03 12:10 in Propeller 1
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

  • Mike GreenMike Green Posts: 23,101
    edited 2010-10-03 10:16
    Converting a number to its decimal representation requires multiplication and division by 10 which may be simple or not depending on the processor involved.

    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.
  • RavenkallenRavenkallen Posts: 1,057
    edited 2010-10-03 10:29
    Binary(This is one of many uses) is used if you want to easily represent the I/O pins when you are accessing a whole port...Instead of trying to figure out what decimal number would equal a certain amount of pins, you could just say something like...

    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..
  • bill190bill190 Posts: 769
    edited 2010-10-03 10:46
    The most time consuming thing when reading another person's code or my own code is "translating" what the code does to what the data sheet says the settings can be.

    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?
  • 172heavy172heavy Posts: 55
    edited 2010-10-03 11:31
    Thank you , I know that was probably a newbie question, I was just curious.
  • potatoheadpotatohead Posts: 10,261
    edited 2010-10-03 12:10
    It's a good question.

    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.
Sign In or Register to comment.