Urgent C++ question

UlricT

Golden Member
Jul 21, 2002
1,966
0
0
I simply cannot figure this out:

I am doing some work on hex values, and I need to output values as sets of 2: 00 FF 03 4D etc...
My problem is that the values with a leading 0 (04 00 0F etc) are coming out only as 4 0 F.

Here is what I am using to output:
cout << hex << (buffer&0xff)<<endl;

any suggestions as to what I can do???
 

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
Originally posted by: UlricT
I simply cannot figure this out:

I am doing some work on hex values, and I need to output values as sets of 2: 00 FF 03 4D etc...
My problem is that the values with a leading 0 (04 00 0F etc) are coming out only as 4 0 F.

Here is what I am using to output:
cout << hex << (buffer&0xff)<<endl;

any suggestions as to what I can do???

Store it as a string? Sorry, dunno C++, but free bump. :p
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Try to use sprintf to format the string prior to output.
 

UlricT

Golden Member
Jul 21, 2002
1,966
0
0
got it... used some dumb if statements to get around the problem. Basically converted to a decimal... compared whether < 16, and if so put a "0" at the beginning :p
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: UlricT
got it... used some dumb if statements to get around the problem. Basically converted to a decimal... compared whether < 16, and if so put a "0" at the beginning :p
Another way would be to add 256 to the origbinal number, conber it to hex and stip off the leading character. No if required