Discussion:
unsigned char to char and vice versa
(too old to reply)
Miha Nedok
2004-08-01 13:10:17 UTC
Permalink
How can I properly convert unsigned char to char and char to unsigned char ?

-Mike
Doug Harrison [MVP]
2004-08-01 14:55:39 UTC
Permalink
Post by Miha Nedok
How can I properly convert unsigned char to char and char to unsigned char ?
Use static_cast. For pointers to those types, it would be reinterpret_cast.
--
Doug Harrison
Microsoft MVP - Visual C++
Miha Nedok
2004-08-01 15:41:27 UTC
Permalink
Post by Doug Harrison [MVP]
Post by Miha Nedok
How can I properly convert unsigned char to char and char to unsigned char ?
Use static_cast. For pointers to those types, it would be reinterpret_cast.
Miha Nedok
2004-08-01 15:43:19 UTC
Permalink
I tried to convert char to unsigned char and then to char and print it
and got:
0xfffffff8
Expression could not be evaluated

Can some one give me please a short sample that works ?



-Mike
Post by Doug Harrison [MVP]
Post by Miha Nedok
How can I properly convert unsigned char to char and char to unsigned char ?
Use static_cast. For pointers to those types, it would be reinterpret_cast.
Doug Harrison [MVP]
2004-08-01 17:47:45 UTC
Permalink
Post by Miha Nedok
I tried to convert char to unsigned char and then to char
That's a lossless, exact conversion sequence in VC++ and every other
compiler I know of.
Post by Miha Nedok
0xfffffff8
That's the result of promotion of char to int. This "integral promotion"
happens in many contexts, including passing char to the ellipsis as with
printf, and in arithmetic expressions. By default, plain char is signed in
VC++, represented with two's complement, so a char with value 0xf8 is
interpreted as a negative number, and promotion of char to int is performed
by simple sign extension.
Post by Miha Nedok
Expression could not be evaluated
Don't know what told you that. Sounds like the debugger, and if so, it's
irrelevant.
Post by Miha Nedok
Can some one give me please a short sample that works ?
What sort of behavior are you expecting from the compiler? How does what
you're observing differ from it? You need to define what you mean by
"works".
--
Doug Harrison
Microsoft MVP - Visual C++
Miha Nedok
2004-08-01 20:37:00 UTC
Permalink
Yes that's the debugger. The idea was to print the converted unsigned
char to char.

My problem is: Calling a function that expects a unsigned char and my
data get's "garbled" because the output isn't OK. Actually I'm calling
openssl's functions for encrypying. I get no error from them. But when
decrypting the data it's no really OK... some chars are added on the
beggining and on the end.

But thanks Doug.

-Mike
Post by Doug Harrison [MVP]
Post by Miha Nedok
I tried to convert char to unsigned char and then to char
That's a lossless, exact conversion sequence in VC++ and every other
compiler I know of.
Post by Miha Nedok
0xfffffff8
That's the result of promotion of char to int. This "integral promotion"
happens in many contexts, including passing char to the ellipsis as with
printf, and in arithmetic expressions. By default, plain char is signed in
VC++, represented with two's complement, so a char with value 0xf8 is
interpreted as a negative number, and promotion of char to int is performed
by simple sign extension.
Post by Miha Nedok
Expression could not be evaluated
Don't know what told you that. Sounds like the debugger, and if so, it's
irrelevant.
Post by Miha Nedok
Can some one give me please a short sample that works ?
What sort of behavior are you expecting from the compiler? How does what
you're observing differ from it? You need to define what you mean by
"works".
Scott McPhillips [MVP]
2004-08-01 23:01:13 UTC
Permalink
Post by Miha Nedok
Post by Miha Nedok
0xfffffff8
My problem is: Calling a function that expects a unsigned char and my
data get's "garbled" because the output isn't OK. Actually I'm calling
openssl's functions for encrypying. I get no error from them. But when
decrypting the data it's no really OK... some chars are added on the
beggining and on the end.
I think you are misinterpreting what is happening. 0xfffffff8 does not
mean some chars are added. It means that when you attempted to display
the char (or unsigned char) you used a formatting function that first
converts it to an int.

char and unsigned char are 8-bit quantities. Converting from one to the
other does not change the bits - only the arithmetic rules that will be
used in expressions. 0xfffffff8 is a 32-bit quantity, not "some chars
added on the beginning".

You can display the hex value of a char or unsigned char with
printf("%02x", c).
--
Scott McPhillips [VC++ MVP]
Miha Nedok
2004-08-02 05:07:57 UTC
Permalink
Hi !

I wrote it a little bit of strange. The output was from the debugger.

So I managed to print the value of the unsigned char and it seems to be
OK. So the problem must be with the openssl libraries while decrypting.
Thanks to Doug and to you for your help and sorry for waisting your time.


-Mike
Post by Scott McPhillips [MVP]
Post by Miha Nedok
Post by Miha Nedok
0xfffffff8
My problem is: Calling a function that expects a unsigned char and my
data get's "garbled" because the output isn't OK. Actually I'm calling
openssl's functions for encrypying. I get no error from them. But
when decrypting the data it's no really OK... some chars are added on
the beggining and on the end.
I think you are misinterpreting what is happening. 0xfffffff8 does not
mean some chars are added. It means that when you attempted to display
the char (or unsigned char) you used a formatting function that first
converts it to an int.
char and unsigned char are 8-bit quantities. Converting from one to the
other does not change the bits - only the arithmetic rules that will be
used in expressions. 0xfffffff8 is a 32-bit quantity, not "some chars
added on the beginning".
You can display the hex value of a char or unsigned char with
printf("%02x", c).
Miha Nedok
2004-08-02 05:07:18 UTC
Permalink
Hi !

I wrote it a little bit of strange. The output was from the debugger.

So I managed to print the value of the unsigned char and it seems to be
OK. So the problem must be with the openssl libraries while decrypting.
Thanks to Doug and to you for your help and sorry for waisting your time.


-Mike
Post by Scott McPhillips [MVP]
Post by Miha Nedok
Post by Miha Nedok
0xfffffff8
My problem is: Calling a function that expects a unsigned char and my
data get's "garbled" because the output isn't OK. Actually I'm calling
openssl's functions for encrypying. I get no error from them. But
when decrypting the data it's no really OK... some chars are added on
the beggining and on the end.
I think you are misinterpreting what is happening. 0xfffffff8 does not
mean some chars are added. It means that when you attempted to display
the char (or unsigned char) you used a formatting function that first
converts it to an int.
char and unsigned char are 8-bit quantities. Converting from one to the
other does not change the bits - only the arithmetic rules that will be
used in expressions. 0xfffffff8 is a 32-bit quantity, not "some chars
added on the beginning".
You can display the hex value of a char or unsigned char with
printf("%02x", c).
Doug Harrison [MVP]
2004-08-01 22:18:48 UTC
Permalink
Post by Miha Nedok
Yes that's the debugger. The idea was to print the converted unsigned
char to char.
My problem is: Calling a function that expects a unsigned char and my
data get's "garbled" because the output isn't OK. Actually I'm calling
openssl's functions for encrypying. I get no error from them. But when
decrypting the data it's no really OK... some chars are added on the
beggining and on the end.
Briefly show how you're doing your output. All I can think of is that you're
using something like printf("%x"), and if your output is the "0xfffffff8"
you mentioned last time, you might be passing a char variable equal to 0xF8
(i.e. -8) to printf, which is subject to the integral promotion rule I
mentioned last time. In other words, your char contains -8, and when
promoted to int, the int result contains -8, which has the representation
0xFFFFFFF8 in two's complement. To fix that, you would say:

// x is a char
printf("%x\n", (unsigned char) x);

Above, x is interpreted as unsigned char for the integral promotion, and the
result of the promotion is 0xF8 (but contained in an int), which is OK to
pass to "%x".
--
Doug Harrison
Microsoft MVP - Visual C++
Loading...