-
January 28th, 2009, 07:53 AM
#1
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.
-
January 28th, 2009, 11:27 AM
#2
Re: Problem printing values as hex
Originally Posted by Flembobs
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
-
January 28th, 2009, 12:00 PM
#3
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("%08x ", MDBuffer[i]);
?
-
January 28th, 2009, 04:33 PM
#4
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;
}
-
January 29th, 2009, 04:35 AM
#5
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
-
January 30th, 2009, 08:43 AM
#6
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("%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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|