Problem printing values as hex
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Problem printing values as hex

  1. #1
    Join Date
    Jan 2009
    Posts
    2

    Problem printing values as hex

    I'm trying to print out the following 128-bit value as a sequence of 8-bit values in hexadecimal:

    Code:
    unsigned int MDBuffer [4] = {0x01234567, 0x89abcdef, 0xfedcba98, 0x76543210};
    The function I'm using to print the values:

    Code:
    void printOutput()
    {
       unsigned char * outChars = (unsigned char*) &MDBuffer;
       for(int i = 0; i<16; i++)
       {
          printf("%02x " , *(outChars+i) );
       }
       cout<<"\n";
    }
    This produces the output:
    67 45 23 01 ef cd ab 89 98 ba dc fe 10 32 54 76

    I'm not sure what is happening to produce this output...

    My only guess is that it relates to the 'endianess' of the values, and that my function is printing them 'backwards'.

    Is this guess along the right lines, or am I making some other mistake? What could I do to correct this bug?

    I'm currently researching the whole topic of endianess in C++, but it's all pretty much over my head! So any help would be greatly appreciated.

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,725

    Re: Problem printing values as hex

    Quote Originally Posted by Flembobs View Post
    My only guess is that it relates to the 'endianess' of the values, and that my function is printing them 'backwards'.
    Yes, that's correct.
    The machine you are using is 'little endian', which means that it stores numbers in memory in lowest to highest significant byte order.

    If you want to extract bytes in the correct order regardless of 'endianness' then use shifting and masking.
    Code:
    unsigned int i = 0x01234567;
    
    byte0 = (i >> 24) & 0xFF; // 0x01
    byte1 = (i >> 16) & 0xFF; // 0x23
    byte2 = (i >> 8) & 0xFF; // 0x45
    byte3 = i & 0xFF; // 0x67
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  3. #3
    Join Date
    Aug 2007
    Posts
    858

    Re: Problem printing values as hex

    Is this guess along the right lines, or am I making some other mistake? What could I do to correct this bug?
    Is there some reason you don't simply do

    Code:
    for (int i = 0; i < 4; ++i)
      printf("&#37;08x ", MDBuffer[i]);
    ?

  4. #4
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: Problem printing values as hex

    My compiler wouldn't even print anything via the printf. I don't think it is a good idea to mix printf and cout in the same program. I may not have included the correct headers or for some reason the output stream wasn't being flushed in the posted program. You could just do it with with std::cout.

    Code:
    int main ()
    {
        unsigned int MDBuffer [4] = {0x01234567, 0x89abcdef, 0xfedcba98, 0x76543210};
        unsigned char * outChars = (unsigned char*) &MDBuffer;
    
        // prints backwards
        for(int i = 0; i<16; i++)
        {
    	std::cout << std::hex << std::setfill('0') << std::setw(2) << static_cast<short>(*(outChars+i));
        }
        std::cout << std::endl;
    
       // prints forwards.  This outputs the data in the way that you wanted it.
        for(int i = 0; i<4; i++)
        {
    	std::cout << std::hex << std::setfill('0') << std::setw(8) << MDBuffer[i];
        }
        
        std::cout << std::endl;
    
        return 0;
    }

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,725

    Re: Problem printing values as hex

    Code:
    // prints backwards
    for(int i = 0; i<16; i++)
    {
        std::cout << std::hex << std::setfill('0') << std::setw(2) << static_cast<short>(*(outChars+i));
    }
    Only for 'little endian' machines. For 'big endian' machines it will print forwards.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  6. #6
    Join Date
    Jan 2009
    Posts
    2

    Re: Problem printing values as hex

    Thanks for your help. My eventual solution was this:
    Code:
    void printOutput()
    {
       
       for(int i = 0; i<4; i++)
       {
          unsigned char * outChars = (unsigned char*) &MDBuffer[i];
       
          for(int j = 3; j>-1; j--)
          {
             printf("&#37;02x " , *(outChars+j) );
          }
       }
       cout<<"\n";
    }
    It's probably not optimal, and wouldn't work on a big-endian machine... but since I'm only doing this as a programming exercise, it's probably good enough!

    I think I understand the problem and some possible solutions now anyways, so thanks. All this code-portability stuff drives me mad!

    Is there some reason you don't simply do

    Code:
    for (int i = 0; i < 4; ++i)
      printf("%08x ", MDBuffer[i]);
    1) I didn't think of it *facepalm*.
    2) I wanted the output to be split up in groups of two to make it more readable anyways.
    Last edited by Flembobs; January 30th, 2009 at 08:51 AM.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center