views:

456

answers:

2

I'm writing a datalog parser for a robot controller, and what's coming in from the data log is a number in the range of 0 - 65535 (which is a 16 bit unsigned integer if I'm not mistaken). I'm trying to convert that to a signed 16 bit integer to display to the user (since that was the actual datatype before the logger changed it).

Can someone give me a hand?

Example:

What the values should be (0, -1, -2, -3, -4)

What the values are (0, 65535, 65534, 65533, 65532)

+3  A: 

Have you tried explicit casting?

UInt16 x = 65535;
var y = (Int16)x; // y = -1
pblasucci
You're a genius, thanks!
Dylan Vester
A: 

how about if it's not zero, negate it and add 65534

ie

0 -> 0
65535 -> -65535 + 65534 = -1
John Boker