Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert an unsigned 16 bit int to a signed 16 bit int in C#

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)

like image 972
Dylan Vester Avatar asked Jan 31 '10 04:01

Dylan Vester


People also ask

How do I cast an unsigned int to signed int?

To convert a signed integer to an unsigned integer, or to convert an unsigned integer to a signed integer you need only use a cast. For example: int a = 6; unsigned int b; int c; b = (unsigned int)a; c = (int)b; Actually in many cases you can dispense with the cast.

What is the value of 16-bit unsigned integer?

A 16-bit integer can store 216 (or 65,536) distinct values. In an unsigned representation, these values are the integers between 0 and 65,535; using two's complement, possible values range from −32,768 to 32,767.

What is a signed 16-bit integer?

Signed Integer: A 16-bit signed integer ranging from -32,768 to +32,767.


2 Answers

Have you tried explicit casting?

UInt16 x = 65535;
var y = (Int16)x; // y = -1
like image 63
pblasucci Avatar answered Oct 12 '22 23:10

pblasucci


Using unchecked here avoids a crash if [X] Check for Arithmetic Overflow is on:

UInt16 x = 65535;
Int16 y = unchecked((Int16)x);
like image 23
Christoph Avatar answered Oct 13 '22 00:10

Christoph