Formatting iostream for readable hex.

My problem is that the setfill('0') doesn't affect the output;

I want four columns of 64bit numbers in hex which I read in binary mode from a file.
Any help would be appreciated.

Here is the problem:

fffffff2 f1040000 fff10000 780000
76000d 11740000 0 5f
5f40405f 50636853 796d5f40 30304055
66687669 68556867 76657655 776c7866

and the code to produce it:

cout << hex << setw(4) << setfill('0') ;

unsigned long test,inc,grp;
grp = 0;
for (int i = 0; i < size; i += 4){
test = buffer[i];
grp++;
for (inc = 0; inc < 4; inc++) test = (test << 8) | buffer[inc + i];
cout << test << '\t';
if (grp == 4){
cout << endl;
grp = 0;
}
}
Last edited on
why does this forum take away my formatting?
Last edited on
why does this forum take away my formatting?


Because you didn't put the code in code tags.

[code]put your code in here and it'll keep its formatting and syntax highlighting[/code]

Here is the problem:


One of the things I really hate about iostream is that some settings are persistent and some settings aren't.

In this case, hex and setfill are persistent, but setw isn't. This means you have to provide setw each time you provide a number.

IE, you'll want to change this:

cout << test << '\t';

To this:

cout << setw(8) << test << '\t';



Also note:

- You are working with 32-bit numbers, not 64-bit.
- You want setw(8) and not setw(4) because 32-bit numbers have 8 digits, not 4 (each byte is 2 digits)



EDIT:

Also... big endian? What? ;)
Last edited on
Oh. Thanks. I am doing x86_64bit. little-end.
Little endian. Thanks for your help sir.
1
2
3
test = buffer[i];
grp++;
for (inc = 0; inc < 4; inc++) test = (test << 8) | buffer[inc + i];


This logic will display big endian, not little endian.

You are treating the first byte as the most significant -- that is big endian.

For little endian you have to treat the first as the least significant.
Right again. Boy I am rusty. lol Thanks. And yes the class library is quirky like that.
I'm on track now. Thanks.
Topic archived. No new replies allowed.