|
-
May 4th, 2010, 09:49 PM
#1
Endianness problem
Hi, I'm having a problem with my current program. I have it to the point that it will take a long int and give the hex for the variable. However the hex it gives me is backwards because of little endian format. I've messed around with ways to invert the answer and can't come up with anything that works. When is enter 2140118959 the answer I recieve is Af 9f 8f 7f when I want to see 7f 8f 9f Af. Any help? this one has stumped me for awhile. Thanks.
[c++]
#include <iostream>
using namespace std;
void Show_hex(void *p, int); //Function Prototype
//****************main*****************//
int main() {
//Declare and initialize sample arrays and variables
long int long_var;
cout << "\nHexadecimal dump: \n";
cout << "Please enter value to be displayed in hex: ";
cin >> long_var;
//Arrays and variables are passed to hex_dump() function
Show_hex(&long_var, sizeof(long_var));
return 0;
}
//*****************************
// show_hex() function
//*****************************
void Show_hex(void *void_ptr, int elements) {
unsigned char digit, high_nib, low_nib;
for(int x = 0; x < elements; x++) {
digit = *((char *)void_ptr + x);
high_nib = low_nib = digit; //copy in high and low nibble
high_nib = high_nib & 0xf0; // Mask out 4 low-order bits
high_nib = high_nib >> 4; // Shift high bits left
low_nib = low_nib & 0x0f; // Mask out 4 high order bits
//Display in ASCII hexadecimal digits
if(low_nib > 9)
low_nib = low_nib + 0x37;
else
low_nib = low_nib + 0x30;
//Same thing for high nibble
if(high_nib > 9)
high_nib = high_nib + 0x37;
else
high_nib = high_nib + 0x30;
cout << high_nib << low_nib << " ";
}
cout << "\n";
system("pause");
return;
}
[/c++]
-
May 4th, 2010, 11:27 PM
#2
Re: Endianness problem
I don't suppose something like:
Code:
cout << setfill('0') << setw(8) << uppercase << hex << 42;
is the answer?
-
May 4th, 2010, 11:39 PM
#3
Re: Endianness problem
Just change the loop
Code:
for (int x = elements - 1; x >= 0; --x)
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
|