This is how singed/unsigned numbers are represented and converted.
It looks like C compiler's default in your case use signed byte
as underlying type for char
(since you are note explicitly specifying unsigend char
compiler's default is used, See - Why is 'char' signed by default in C++? ).
So 191 (0xBF) as signed byte means negative number (most significant bit is 1) - -65
.
If you'd use unsigned char
value would stay positive as you expect.
If your compiler would you wider type for char
(i.e. short
) that 191 would stay as positive 191 irrespective of whether or not char
is signed or not.
In C# where it always unsigned - see MSDN char:
Type: char
Range: U+0000 to U+FFFF
So 191 will always convert to to int
as you expect.