Skip to main content
GameDev.net gamedev.net
🔒 Locked

Casting Unsigned Char to Char?

Started by 6 Jan 19, 2007 at 9:41 PM 10 replies 8.5k views
Original Post
6
6
Hey all, In RakNet, packets of data come with the data stored as an Unsigned Char. I'd like to print this data using a function that requires data passed to it as a signed char. Anyone know how to convert an Unsigned Char to Signed Char? I've searched everywhere and can't seem to find anything. Cheers, 6
----------------------------------------------------
Check out my casual TBS game blog
----------------------------------------------------
Vorpy
Vorpy
What language?

I think in C++ the only safe way is to use a reinterpret_cast. Assuming 8 bit chars, values greater than 127 would become (value - 256) because of the difference in how the bits are interpreted. If you tried to static_cast such a value, the result would be undefined instead.

Another alternative is to static_cast it to a signed type that is bigger than a char and could hold all of the values, then use a function that prints out that type instead.
jflanglois
jflanglois
Boost Numeric Conversion does this.

TheFunction( boost::numeric_cast< signed char >( arg ) );

You could also use the converter class directly:

boost::numeric::converter< signed char, unsigned char > c;
TheFunction( c( arg ) );


The default behavior is that the converter will throw an exception if there was an overflow.


jfl.
Julian90
Julian90
Quote:
I think in C++ the only safe way is to use a reinterpret_cast


reinterpret_cast is only defined for integral type -> pointer, pointer -> integral type, and pointer -> pointer
6
6
The reiterpret_cast does compile but the program crashes on that particular line so it doesn't really work for me unfortunately.

The boost library was confusing and I couldn't get it to work at all. It also seems a bit overkill for something which I see should be quite simple.

Is there not an easyway to iterate through p->data (data being and unsigned char array) and add -127 to it and store the result in a signed char array?
----------------------------------------------------
Check out my casual TBS game blog
----------------------------------------------------
Vorpy
Vorpy
Reinterpret casting an unsigned char to a signed char crashes the program? Weird, I thought it would just perform some undefined operation that would probably be what you wanted. I guess I sort of lied before when I said it was a safe way, but I'm still surprised it crashed.

I think now I've thought of a real safe way to do what you want. static_cast the unsigned char to a larger numeric type, subtract 128, and then static_cast the result to a signed char. Of course this is only safe if you know that a certain type is definitely bigger than a char, but it would be a very odd platform that didn't have something like that availabe. And subtract 128, not 127!
Anon Mike
Anon Mike
reinterpret_cast can't crash. If you're getting a crash on the same line for some reason then it's some other problem.

The simplest thing to do would probably be to do things the old fashioned way with "s = (char) u;".
-Mike
Dancin_Fool
Dancin_Fool
I agree with the above poster. For the purposes of what you're trying to do, I would wager to guess you would be fine using:

char cValue = (char)ucValue;
Julian90
Julian90
Quote:
The reiterpret_cast does compile but the program crashes on that particular line so it doesn't really work for me unfortunately.

Quote:
Reinterpret casting an unsigned char to a signed char crashes the program? Weird

Quote:
reinterpret_cast can't crash


As i said the result is undefined and invocation of undefined behaviour can legitimatly crash the program, VS05 even goes so far as refusing to compile it.
Enigma
Enigma
Quote:
Original post by Julian90
Quote:
The reiterpret_cast does compile but the program crashes on that particular line so it doesn't really work for me unfortunately.
Quote:
Reinterpret casting an unsigned char to a signed char crashes the program? Weird
Quote:
reinterpret_cast can't crash
As i said the result is undefined and invocation of undefined behaviour can legitimatly crash the program, VS05 even goes so far as refusing to compile it.

The result is not undefined, it's unspecified. In cases like this (where data has been packed into unsigned chars for network transport) reinterpret_cast is often the correct solution, although usually you would reinterpret_cast the array rather than individual unsigned chars. If you do need to reinterpret individual unsigned chars then reinterpret_cast to a reference: signed char result = reinterpret_cast< signed char & >(source);

Σnigma

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.