Results 1 to 24 of 24

Thread: [RESOLVED] Decimal vs. Hex - The differences?

  1. #1

    Thread Starter
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Resolved [RESOLVED] Decimal vs. Hex - The differences?

    Hello all,
    I've been puzzled with this question for while now: what is the differences (i.e. advantages of using one over the other, speed, or anything) between using hex values versus decimal values as in this context:
    Code:
    'Using hex 
    Dim X As Integer = &H7B
    
    'Using decimal
    Dim X As Integer = 123
    To me, the prefered way would be using decimal since it is much easier to read. However, I see a lot of developers use hex... Just wonder why?

    Hope someone can shed a light on this.

  2. #2
    Addicted Member
    Join Date
    Nov 2007
    Location
    Belgium
    Posts
    154

    Re: Decimal vs. Hex - The differences?

    a computer can read hex faster than decimal. Actually it can read binairie better but that's to hard to read for us so hex is a sort of in between solution.

  3. #3

    Thread Starter
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Decimal vs. Hex - The differences?

    Quote Originally Posted by kevin hash
    a computer can read hex faster than decimal. Actually it can read binairie better but that's to hard to read for us so hex is a sort of in between solution.
    This is true, and it may give some slight speed advantages back in the 286 processors' days... However, with today's processor speed, I would doubt if you can detect any speed differences... Besides, I haven't seen anyone code written this way:
    Code:
    For i As Integer = &H0 To &HFF23
        'Do something
    Next
    My question is, if using hex helps boosting execution speed, why is it not being used throughout the whole application but mainly just on constant variables?

    Anything else besides speed?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Decimal vs. Hex - The differences?

    its probably a throwback to the original 8086 + beyond
    Last edited by .paul.; Jan 16th, 2008 at 05:44 PM.

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Decimal vs. Hex - The differences?

    Computers cannot read hex, binary (when displayed with 0's and 1's), decimal or anything faster.

    Hex is used to represent a number in a way where 16's mean something.

    Colors have 16 meaning.

    ASCII codes have 16 meaning.

    So that is the whole reason - the only reason - to ever represent something in hex.

    See this thread if you are interested in some reasons.

    http://www.vbforums.com/showthread.p...hex+16+columns

    These are all just visual representations. Everything is interpreted during the compile process - and of course - stored in binary.

    [edit] clarified the "binary" issue in the first sentence [/edit]
    Last edited by szlamany; Jan 16th, 2008 at 08:29 PM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Decimal vs. Hex - The differences?

    As szlammy says, whether you use a decimal representation for your numbers or hexadecimal makes no difference to anything except the way you see the number.

    The advantage of hexadecimal is that every two digits represents a byte. A 32-bit number in hex is 8 digits and each pair of digits represents 8 bits or 1 byte. That's much cleaner than a decimal numbers, that have no "connection" to bits and bytes.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Decimal vs. Hex - The differences?

    Most of the time a number represented in hex takes less memory since it is composed of less 'characters'. I read somewhere that this was the main reason back then. I would imagine that data files are advantageously smaller this way, esp. if memory is more expensive than computing power (like 30 years ago).
    VB 2005, Win Xp Pro sp2

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Decimal vs. Hex - The differences?

    Quote Originally Posted by Half
    Most of the time a number represented in hex takes less memory since it is composed of less 'characters'. I read somewhere that this was the main reason back then. I would imagine that data files are advantageously smaller this way, esp. if memory is more expensive than computing power (like 30 years ago).
    That doesn't relate to the original question though. When you're talking about literals in code there is absolutely no difference except the visual difference because it's all binary when compiled.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Decimal vs. Hex - The differences?

    Quote Originally Posted by Half
    Most of the time a number represented in hex takes less memory since it is composed of less 'characters'. I read somewhere that this was the main reason back then. I would imagine that data files are advantageously smaller this way, esp. if memory is more expensive than computing power (like 30 years ago).
    Let's be clear.

    Nothing is ever stored in hex.

    It's only possible to store in binary - that's the whole point of magnetic recording media - whether it's disk, floppy or a 2GB memory stick.

    So - we have bytes - 8 bits of memory. That's been a standard "size" for a long long time. Obviously you can store 256 different values in that 8 bit spot.

    Now it's what you make those values represent that's important - not hex or decimal - but what they represent.

    If you have 256 different characters - letters, numbers, symbols, etc - then you are using that one byte for a single "visual" entity. This is called character encoding.

    That's expensive - especially as you said - 30 years ago (actually more like 50 years ago!)...

    Back in my Digital PDP-11 mini days we had something called RADIX-50. Goal was basically to only store A-Z and 0-9 (and 6 symbols as well) - that's a lot less then 256 different characters. Since you didn't need all 8 bits to "encode" a character. You could actually store 3 characters in 2 bytes (or a 16 bit WORD).

    http://en.wikipedia.org/wiki/RADIX-50

    IBM and COBOL make extensive use of COMP fields - compressed numeric fields. If you are only storing digits and a plus/minus sign you can make much better use of the bits - not wasting 8 bits for each encoded character.

    As for speed - if there are not machine-level instructions to deal with these encoding schemes they need to be converted to schemes that can be processed.

    The real historical use of these odd encoding schemes comes from punch cards.

    http://www.cs.uiowa.edu/~jones/cards/codes.html

    There you only had 12 rows for each column - so you had better come up with encoding schemes that maximized what you were storing.

    And of course that wonderful old media called paper tape

    http://www.merrymeet.com/minow/papertape/papertape.html

    I've used them all...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10
    Addicted Member
    Join Date
    Nov 2007
    Location
    Belgium
    Posts
    154

    Re: Decimal vs. Hex - The differences?

    I study informatics. And in the hardware class we are now learning about coding , decoding and the difference between all those codes like hex, dec, octa,... But the things you just said are completely new to me. I didn't know that before. Thanks for that:d

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Decimal vs. Hex - The differences?

    You're actually learning about octal?
    My usual boring signature: Nothing

  12. #12

    Thread Starter
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Decimal vs. Hex - The differences?

    Very interesting information from veryone, especially Szlamany... So basically, since everything is compiled to machine language, it's pretty safe to assume that regardless of what base we use to code (decimal, hex, octal... you name it), it's not going to make any difference. Is that right?

  13. #13
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Decimal vs. Hex - The differences?

    Quote Originally Posted by stanav
    Very interesting information from veryone, especially Szlamany... So basically, since everything is compiled to machine language, it's pretty safe to assume that regardless of what base we use to code (decimal, hex, octal... you name it), it's not going to make any difference. Is that right?
    Yes - that is basically true.

    When you assign a value to a variable in code - that literal can be pretty much any base.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  14. #14
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Decimal vs. Hex - The differences?

    Do we know for a fact that the compiler converts hex to bin with the same speed as dec to bin? I quess this would be the single advantage over the one way or the other no matter how small.
    VB 2005, Win Xp Pro sp2

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Decimal vs. Hex - The differences?

    Hardly matters, since the conversion happens prior to runtime, but if you are wondering about the IL, all numbers are in hex in IL code. A decimal litteral shows up as Hex.
    My usual boring signature: Nothing

  16. #16
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Decimal vs. Hex - The differences?

    You would have to examine formulas and techniques for converting from one base to another.

    Hex converts to nibbles (half a byte) rather nicely - but I'm not sure how you could determine how the IL hex values get loaded into memory...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Decimal vs. Hex - The differences?

    Same as any others. ASM doesn't use binary, either, so hex is as far as we need to follow it. A decimal literal in code is converted to a hex in IL, and that's what matters.
    My usual boring signature: Nothing

  18. #18
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Decimal vs. Hex - The differences?

    But the CPU processes only binary - so the hex literals have to be loaded into binary memory storage - but we are probably talking about op-codes running on the chip anyway to accomplish this.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  19. #19
    Addicted Member
    Join Date
    Nov 2007
    Location
    Belgium
    Posts
    154

    Re: Decimal vs. Hex - The differences?

    Quote Originally Posted by Shaggy Hiker
    Hardly matters, since the conversion happens prior to runtime, but if you are wondering about the IL, all numbers are in hex in IL code. A decimal litteral shows up as Hex.
    What is IL?

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Decimal vs. Hex - The differences?

    Quote Originally Posted by kevin hash
    What is IL?
    Intermediate Language. VB.NET, C#, etc. code is compiled to MSIL, which is then compiled to machine code by the .NET Framework as it's executed. It's the .NET equivalent of Java bytecode.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  21. #21
    Addicted Member
    Join Date
    Nov 2007
    Location
    Belgium
    Posts
    154

    Re: Decimal vs. Hex - The differences?

    thanks for the information

  22. #22
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Decimal vs. Hex - The differences?

    In interpreted languages such as PHP and Perl - the characters need to be converted to a number. I guess you could say that the less characters you need to convert the quicker it will be converted.

    What you should however pay attention to is the types of variables you are using. When it comes down to values such as bytes there is an argument that storing them as a 32bit / 64bit integer is quicker than storing them as an 8bit integer as the whole thing can be thrown threw the processor at once. I am not sure of the turth in this but it hardly matters theis days .

    As for floating point numbers - only use them if you need to. Single precision weighs in at 8 bytes and double prescision at 16 bytes. The whole number also comes in several urls. Floating point operations are somewhat slower than binary operations.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  23. #23

    Thread Starter
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Decimal vs. Hex - The differences?

    From the many participants' inputs in this thread, we can now conclude that it's not making any differences whether we use binary, octal, decimal or hex bases to write literals in our code. Unless someone can give a good reason why we should ever use any bases other than decimal, I'll keep using decimal because it's easier to read (at least with me). And I will mark this thread as resolved for now.
    Thanks everyone for sharing your knowledge.

  24. #24
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] Decimal vs. Hex - The differences?

    There are some kinds of data where Hex is easier to read, such as colour numbers: &H00FF00 is fully Green (the R and B components are 0, the G component is at the maximum); but the decimal equivalent 65280 isn't anywhere near as clear.

    Similar applies to the numbers used by various API's, but in general decimal numbers are the easiest to read.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width