Thread: why do we need hex ?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    why do we need hex ?

    i m srry i wanted to ask why do we need hexdecimal in programs instead of just writing the real number ? because its not like instructions or anything i mean like for instance binary can contain inside ints so we can use them to send pieces of information by compressing binries inside ints but why do we need hex ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    We don't need hexadecimal representation other than for convenience.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because hex [and octal] and binary are closely related.

    If I say to you, what is 4711 in binary, I expect you'll take little bit of time to come up with the correct answer (as a 16-bit number it is 0001 0010 0110 0111)

    If on the other hand I tell you that hex digits translate to binary as follows
    Code:
    0 = 0000
    1 = 0001
    2 = 0010
    3 = 0011
    4 = 0100
    5 = 0101
    6 = 0110
    7 = 0111
    8 = 1000
    9 = 1001
    A = 1010
    B = 1011
    C = 1100
    D = 1101
    E = 1110
    F = 1111
    and asked you to translate 1267, what how long would it take you?

    Try both, and tell me which is quicker...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Because computers work in binary and hex translates very well to binary (binary itself would be somewhat too hard to read for humans). It's much harder to convert decimals to binary in one's head.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    4F3 answer <<
    so when we put a hex number than a normal int it translates faster than binary so its somewhat sufficient ?

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    and because 2 hex digits fix perfectly into 1 byte.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    147
    so when we put a hex number than a normal int it translates faster than binary so its somewhat sufficient ?
    It's not faster, just convenient for reading.

    Depending on how you think of it, all of your base 10 constants get converted into binary (or hex) when compiled.
    Last edited by JVene; 04-27-2009 at 01:29 PM.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Simply put, if you want to describe bits (binary digits), hex is excellent, e.g. 2^10 = 0x400 = 1024 - they all become exactly the same for the computer 0010 0000 0000 is the binary value. If you want to say one thousand, then 0x3E8 is just cryptic. But if you want to know which bits are set in a "random" number, such as 43, then it's not so easy. It's much easier if the number is 0x2B - it turns out to be 0010 1011.

    Sorry, I missed a bit of my "challenge" in the original reply: Convert 0x1267 to binary. Also convert 4711 in decimal to binary. Which will you do faster (not using a calculator or computer)?

    There is absolutely not NECESSITY to use hex - it's just a convenient way to describe large binary valules without loosing the binary-ness.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You guys totally missed the opportunity to play the "to summon demons" joke.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by elwad View Post
    i m srry i wanted to ask why do we need hexdecimal in programs instead of just writing the real number ?
    What makes you think that writing the number in decimal is more "real" than writing it in hex?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by quzah View Post
    You guys totally missed the opportunity to play the "to summon demons" joke.


    Quzah.
    to summon satan himself, either use hex or listen to britney spears...! now pheer phoolis mortal!!

    (it kinda sucked i know...)
    Last edited by creeping death; 04-28-2009 at 12:23 AM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by quzah
    You guys totally missed the opportunity to play the "to summon demons" joke.
    So, who wants to buy my hex inverters?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ascii to hex and hex to Ascii
    By beon in forum C Programming
    Replies: 1
    Last Post: 12-26-2006, 06:37 AM
  2. Hex Editing help and information please...
    By SG57 in forum C Programming
    Replies: 9
    Last Post: 06-25-2006, 12:30 AM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. Replies: 3
    Last Post: 01-23-2006, 07:25 PM
  5. Is binary HEX?
    By Budgiekarl in forum Tech Board
    Replies: 11
    Last Post: 11-23-2003, 09:02 AM