views:

90

answers:

3

I'm getting a hex string that needs to be converted to a signed 8-bit integer. Currently I'm converting using Int16/Int32, which will obviously not give me a negative value for an 8-bit integer. If I get the value 255 in Hex, how do I convert that to -1 in decimal? I assume I want to use an sbyte, but I'm not sure how to get that value in there properly.

+1  A: 

You need to perform an unchecked cast, like this:

sbyte negativeOne = unchecked((sbyte)255);
SLaks
Why was this downvoted?
SLaks
+3  A: 

You can use Convert.ToSByte

For example:

string x = "aa";
sbyte v = Convert.ToSByte(x, 16);
SwDevMan81
A: 

My solution was to put the first take the first 8 bits of the 16 bit integer and store them in an sbyte.

alexD