Binary converting byte to sbyte
-
Hi coders ! I need to binary convert a byte (unsigned 8 bit, 0 to 255) to an sbyte (signed 8 bit, -128 to 127). I have no problem converting an array of bytes to any other type using BitConverter.To[type] functions, but there's no BitConverter.ToSByte .... VS 2003 - compact framework SP3 any ideas ? Ciao Marco -- modified at 9:54 Thursday 26th January, 2006
-
Hi coders ! I need to binary convert a byte (unsigned 8 bit, 0 to 255) to an sbyte (signed 8 bit, -128 to 127). I have no problem converting an array of bytes to any other type using BitConverter.To[type] functions, but there's no BitConverter.ToSByte .... VS 2003 - compact framework SP3 any ideas ? Ciao Marco -- modified at 9:54 Thursday 26th January, 2006
-
You can just convert it, ignoring overflow, as there is no data loss.
byte source = -42; sbyte dest; unsafe { dest = (sbyte)source; }
--- b { font-weight: normal; }Are you sure ? First off
byte source = -42;
is indeed wrong since byte is UNSIGNED (0 to 255). Second a simple cast doesn't convert BINARY the values, meaning a 10010011 is (let's say) 215 for a byte and -81 for a sbyte. still looknig for help Ciao Marco ;) -
Are you sure ? First off
byte source = -42;
is indeed wrong since byte is UNSIGNED (0 to 255). Second a simple cast doesn't convert BINARY the values, meaning a 10010011 is (let's say) 215 for a byte and -81 for a sbyte. still looknig for help Ciao Marco ;)tried
unckecked
? -
You can just convert it, ignoring overflow, as there is no data loss.
byte source = -42; sbyte dest; unsafe { dest = (sbyte)source; }
--- b { font-weight: normal; }Try to use Convert.ToSByte(source); May be this will work. byte source =42; sbyte dest; dest = convert.ToSByte(source);
-
Are you sure ? First off
byte source = -42;
is indeed wrong since byte is UNSIGNED (0 to 255). Second a simple cast doesn't convert BINARY the values, meaning a 10010011 is (let's say) 215 for a byte and -81 for a sbyte. still looknig for help Ciao Marco ;)Just a typo. Of course a byte can't be -42. It was unchecked (as leppie suggested) that I was going for, not unsafe. However, some testing reveals that you can just cast the value from byte to sbyte. You don't need unchecked mode for casting between signed and unsigned data types.
byte source = 214; sbyte dest; dest = (sbyte)source;
Now dest will contain the value -42.Marco [Stinger] wrote:
Second a simple cast doesn't convert BINARY the values
What do you mean by convert BINARY, then? --- b { font-weight: normal; }
-
Just a typo. Of course a byte can't be -42. It was unchecked (as leppie suggested) that I was going for, not unsafe. However, some testing reveals that you can just cast the value from byte to sbyte. You don't need unchecked mode for casting between signed and unsigned data types.
byte source = 214; sbyte dest; dest = (sbyte)source;
Now dest will contain the value -42.Marco [Stinger] wrote:
Second a simple cast doesn't convert BINARY the values
What do you mean by convert BINARY, then? --- b { font-weight: normal; }
It works perfectly, I did the same test and got the same result. By binary I meant exaclty that : having a brunch of bits read from the sbyte point of view (so to speak) Thanks a LOT everyone !!! Ciao Marco ps: I WANT to install vs 2005 .... it's GREAT, but I have only 1 PC and some soon to be released products in VS 2003. .... I MUST WAIT ..... .... I MUST WAIT ..... .... I MUST WAIT ..... .... I MUST WAIT ..... ;)