This is one of the reasons why C ++ introduced a new casting style that includes static_cast and reinterpret_cast
There are two things that you can say by saying a conversion from signed to unsigned, you can mean that you want the unsigned variable to contain the value of the signed variable modulo the maximum value of your unsigned type + 1. That is, if your signed char has a value -128, then CHAR_MAX+1 added for value 128, and if it has a value of -1, then CHAR_MAX+1 added for value 255, this is what static_cast is doing. On the other hand, you can interpret the value of a memory bit referenced by any variable that should be interpreted as an unsigned byte, regardless of the signed integer representation used in the system, that is, if it has a value of bit 0b10000000 , it should evaluate to values ββ128 and 255 for bit value 0b11111111 , this is done using reinterpret_cast.
Now for two complementary representations, this happens in exactly the same way, since -128 is represented as 0b10000000 , and -1 is represented as 0b11111111 and similarly for all in between. However, other computers (usually older architectures) may use a different signed representation, such as sign and magnitude or addition. In addition to this, bit 0b10000000 will not be -128, but -127, so static casting to unsigned char will make it 129, while reinterpret_cast will make it 128. In addition, in addition to them, 0b11111111 bitvalue will not be -1 , but -0, (yes, this value exists in their complement) and will be converted to a value of 0 with static_cast, but a value of 255 with reinterpret_cast. Please note that in case of addition to them, the unsigned value of 128 cannot actually be represented in the signed char, since it ranges from -127 to 127, due to the value of -0.
I have to say that the vast majority of computers will use two add-ons, which will solve the whole problem almost anywhere in your code. You will probably only ever see systems with something other than two additions in very old architectures, think about the timeframes of the 60s.
The syntax is as follows:
signed char x = -100; unsigned char y; y = (unsigned char)x;
To do this with a good C ++ way with arrays:
jbyte memory_buffer[nr_pixels]; unsigned char* pixels = reinterpret_cast<unsigned char*>(memory_buffer);
or method C:
unsigned char* pixels = (unsigned char*)memory_buffer;