why display is ffffff82 instead of 82 ??

N

news.hku.hk

Hi there,
i have a binary file myfile.bin, the screen output of "od -t x1 myfile.bin"
is

0000000 7f 45 4c .............
0000020 12 13 7f 82
0000024

what i want to get is

7f 45 4c......
12 13 7f 82

but after i use the following commands, the last byte 82 is displayed as
ffffff82 !!??
Here is my commands:

cout << hex << setfill('0');
while (ifs2.get(ch2)) {
cout << int(ch2) << ' ' << setw(2);
}
i'd be grateful if i get any response, thanks a lot
Billy
 
G

Gianni Mariani

news.hku.hk said:
Hi there,
i have a binary file myfile.bin, the screen output of "od -t x1 myfile.bin"
is

0000000 7f 45 4c .............
0000020 12 13 7f 82
0000024

what i want to get is

7f 45 4c......
12 13 7f 82

but after i use the following commands, the last byte 82 is displayed as
ffffff82 !!??

Because:
((signed char) 0x82) is a negative number ...

Convert it to a signed int and you need a similar negative number:
(int)((signed char) 0x82) == 0xffffff82


Here is my commands:

cout << hex << setfill('0');
while (ifs2.get(ch2)) {
cout << int(ch2) << ' ' << setw(2);
}
i'd be grateful if i get any response, thanks a lot


The way to fix this is to do this...

static_cast<int>(static_cast<unsigned char>(ch2))


The built-in type "char" may be signed or unsigned, that is
implementation dependant.
 
M

Martijn Lievaart

On Wed, 07 Jan 2004 15:51:27 +0800, news.hku.hk wrote:

Please don't multipost. I answered your question in acllc-c++, now I see
it again here.

In general, try to stick with one group. If you have to sent to multiple
groups, crosspost (with followup if appropriate).

TIA,
M4
 
G

guinness_tony

news.hku.hk said:
Hi there,
i have a binary file myfile.bin, the screen output of "od -t x1 myfile.bin"
is

0000000 7f 45 4c .............
0000020 12 13 7f 82
0000024

what i want to get is

7f 45 4c......
12 13 7f 82

but after i use the following commands, the last byte 82 is displayed as
ffffff82 !!??
Here is my commands:

cout << hex << setfill('0');
while (ifs2.get(ch2)) {
cout << int(ch2) << ' ' << setw(2);

cout << setw(2)
}
i'd be grateful if i get any response, thanks a lot
Billy

char is (often) a signed integer type. 0x82 is a negative char value
in this case (-126 decimal). Casting to (signed) int retains this
signedness and extends the sign-bit into all available "negativity"
bits in the int (-126 decimal = 0xFFFFFF82). You need to cast the
char to an unsigned-char (range 0..255) before then casting it to an
int to be passed to operator<<

HTH
Tony
 
N

news.hku.hk

sorry about multipost, coz i think different group have different users.
Thanks, all respondants.
 
R

Roland Bengtsson

7f 45 4c......
12 13 7f 82

but after i use the following commands, the last byte 82 is displayed as
ffffff82 !!??
Here is my commands:

cout << hex << setfill('0');
while (ifs2.get(ch2)) {
cout << int(ch2) << ' ' << setw(2);
}
i'd be grateful if i get any response, thanks a lot
Billy

ch2 is converted to signed int in the last output. You have to convert
it to unsigned char instead. This should work:

cout << (unsigned char)(ch2) << ' ' << setw(2);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top