Thread: Displaying and comparing hexadecimal

  1. #1
    Join Date
    Sep 2003

    Displaying and comparing hexadecimal


    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.

    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?

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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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