Displaying and comparing hexadecimal

This is a discussion on Displaying and comparing hexadecimal within the C Programming forums, part of the General Programming Boards category; Hello, I would like to display a hex number and compare with another hex number. I get a hex number ...

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    Displaying and comparing hexadecimal

    Hello,

    I would like to display a hex number and compare with another hex number.

    I get a hex number from the server. And I want to display this and compare with another hex number.

    However, I know you have to use the format specifier x or X. And I guess as I am using a 32 bit cpu. I have been doing this.

    Code:
    printf("%.32x", hexNumber);
    The number I am looking for is this. However there could be other ones. But I just want to compare with this. 00000000000000000000000000000401

    However, shouldn't there be an 'x' displayed like this. 0x00000000000000000000000000000401

    Just one more question how can I compare hex numbers?

    Code:
    if(hexNumber == 0x0000000000000401)
    					{
    /* numbers are the same do something */
    					}
    Many thanks for suggestions,

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    int a = 10;

    if ( a == 10 )

    if ( a == 0xa )

    if ( a == 012 )

    There's no such thing as a "hex number".
    Base is a representation, it doesn't change the meaning.

    The fact that you didn't print an 0x doesn't make it any less comparable to 0x401 or indeed 0x0000401
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-08-2009, 08:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21