Converting byte[] to signed int

J

JT

Need some help in converting a byte[] to a signed int. This is what I
have attempted to do:

byte[] bytes = new byte[3] { 0xFF, 0xFF, 0x9C};
StringBuilder hexString = new StringBuilder();
foreach (byte tmpByte in bytes) hexString.Append(tmpByte.ToString("X2"));

int intValue = int.Parse(hexString.ToString(),
NumberStyles.AllowHexSpecific);

I am getting a positive intValue. However, the intValue is supposed to
be a negative value. Any help would be greatly appreciated.

JT
 
D

Dennis Myrén

I cannot see why you are expecting a negative value.
[BASE16] FFFF9C = [BASE10] 16777116
 
J

JT

Sorry, I should explained my situation abit more. I am getting some
bytes from a device connected to the serial port. The device displays a
negative value and the bytes I am getting for the value is FFFF9C.
Also, the API documentation says that the bytes are in Big-Endian. In
this case, would FFFF9C translate to some negative integer value?
 
J

Jon Skeet [C# MVP]

JT said:
Need some help in converting a byte[] to a signed int. This is what I
have attempted to do:

byte[] bytes = new byte[3] { 0xFF, 0xFF, 0x9C};
StringBuilder hexString = new StringBuilder();
foreach (byte tmpByte in bytes) hexString.Append(tmpByte.ToString("X2"));

int intValue = int.Parse(hexString.ToString(),
NumberStyles.AllowHexSpecific);

I am getting a positive intValue. However, the intValue is supposed to
be a negative value. Any help would be greatly appreciated.

A 24-bit signed int is quite rare - did you really mean it to only have
3 bytes rather than 4?

You can use BitConverter to do the conversion without using a string
format. If that's the wrong endianness, you can use my
EndianBitConverter from
http://www.pobox.com/~skeet/csharp/miscutil
 
D

Dennis Myrén

Then, try:
int result = (a [0x2] << 0x8) + (a [0x1] << 0x10) + (a [0x0] << 0x18);

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
JT said:
Sorry, I should explained my situation abit more. I am getting some bytes
from a device connected to the serial port. The device displays a
negative value and the bytes I am getting for the value is FFFF9C. Also,
the API documentation says that the bytes are in Big-Endian. In this
case, would FFFF9C translate to some negative integer value?
I cannot see why you are expecting a negative value.
[BASE16] FFFF9C = [BASE10] 16777116
 
D

Dennis Myrén

(Where 'a' is represents your variable 'bytes')

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Dennis Myrén said:
Then, try:
int result = (a [0x2] << 0x8) + (a [0x1] << 0x10) + (a [0x0] << 0x18);

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
JT said:
Sorry, I should explained my situation abit more. I am getting some
bytes from a device connected to the serial port. The device displays a
negative value and the bytes I am getting for the value is FFFF9C. Also,
the API documentation says that the bytes are in Big-Endian. In this
case, would FFFF9C translate to some negative integer value?
I cannot see why you are expecting a negative value.
[BASE16] FFFF9C = [BASE10] 16777116
 
D

Dennis Myrén

JT said:
In this case, would FFFF9C translate to some negative integer value?

Yes, it would be -25600 .

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
JT said:
Sorry, I should explained my situation abit more. I am getting some bytes
from a device connected to the serial port. The device displays a
negative value and the bytes I am getting for the value is FFFF9C. Also,
the API documentation says that the bytes are in Big-Endian. In this
case, would FFFF9C translate to some negative integer value?
I cannot see why you are expecting a negative value.
[BASE16] FFFF9C = [BASE10] 16777116
 
L

Liam McNamara

The number given is positive for both big and little endian:

Big: 00FFFF9C
Little: 9CFFFF00

Only when the left-most bit is set will it become negative.

using System.Net;

byte[] data = new byte[] { 0x0, 0xFF, 0xFF, 0x9C };
int bigEndian = BitConverter.ToInt32(data, 0);
int littleEndian = IPAddress.NetworkToHostOrder(bigEndian);

--Liam.

JT said:
Sorry, I should explained my situation abit more. I am getting some
bytes from a device connected to the serial port. The device displays a
negative value and the bytes I am getting for the value is FFFF9C.
Also, the API documentation says that the bytes are in Big-Endian. In
this case, would FFFF9C translate to some negative integer value?
I cannot see why you are expecting a negative value.
[BASE16] FFFF9C = [BASE10] 16777116
 
J

James Curran

What you need to do is "sign-extend" the upper bit from the third byte into
the fourth. (i.e., if the high bit of the highest byte you have is "1",
then every bit of the new fourth byte should be "1". Otherwise they should
be all "0"s)

byte[] data = new byte[] { 0x0, 0xFF, 0xFF, 0x9C };
data[0] = (byte) ((data[1] > 0x7f) ? 0xFF : 0x00);
int littleEndian = BitConverter.ToInt32(data, 0);
int bigEndian= IPAddress.NetworkToHostOrder(littleEndian);
Console.WriteLine( "{0}, {0:x}",bigEndian);

This correct prints out "-100, FFFFFF9C"

Note that Liam had bigEndian & littleEndian reversed in his code.


--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
J

JT

thanks james. another question. in the event, the byte
data is 2 bytes, how would this be handled? thanks.
-----Original Message-----
What you need to do is "sign-extend" the upper bit from the third byte into
the fourth. (i.e., if the high bit of the highest byte you have is "1",
then every bit of the new fourth byte should be "1". Otherwise they should
be all "0"s)

byte[] data = new byte[] { 0x0, 0xFF, 0xFF, 0x9C };
data[0] = (byte) ((data[1] > 0x7f) ? 0xFF : 0x00);
int littleEndian = BitConverter.ToInt32(data, 0);
int bigEndian= IPAddress.NetworkToHostOrder (littleEndian);
Console.WriteLine( "{0}, {0:x}",bigEndian);

This correct prints out "-100, FFFFFF9C"

Note that Liam had bigEndian & littleEndian reversed in his code.


--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

JT said:
Need some help in converting a byte[] to a signed int. This is what I
have attempted to do:

byte[] bytes = new byte[3] { 0xFF, 0xFF, 0x9C};
StringBuilder hexString = new StringBuilder();
foreach (byte tmpByte in bytes) hexString.Append (tmpByte.ToString("X2"));

int intValue = int.Parse(hexString.ToString(),
NumberStyles.AllowHexSpecific);

I am getting a positive intValue. However, the intValue is supposed to
be a negative value. Any help would be greatly appreciated.

JT


.
 
J

James Curran

JT said:
thanks james. another question. in the event, the byte
data is 2 bytes, how would this be handled? thanks.

int littleEndian = BitConverter.ToInt16(data, 0);

In this case the sign-bit is already in the correct place, so know other
modification is needed.

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
-----Original Message-----
What you need to do is "sign-extend" the upper bit from the third byte into
the fourth. (i.e., if the high bit of the highest byte you have is "1",
then every bit of the new fourth byte should be "1". Otherwise they should
be all "0"s)

byte[] data = new byte[] { 0x0, 0xFF, 0xFF, 0x9C };
data[0] = (byte) ((data[1] > 0x7f) ? 0xFF : 0x00);
int littleEndian = BitConverter.ToInt32(data, 0);
int bigEndian= IPAddress.NetworkToHostOrder (littleEndian);
Console.WriteLine( "{0}, {0:x}",bigEndian);

This correct prints out "-100, FFFFFF9C"

Note that Liam had bigEndian & littleEndian reversed in his code.


--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

JT said:
Need some help in converting a byte[] to a signed int. This is what I
have attempted to do:

byte[] bytes = new byte[3] { 0xFF, 0xFF, 0x9C};
StringBuilder hexString = new StringBuilder();
foreach (byte tmpByte in bytes) hexString.Append (tmpByte.ToString("X2"));

int intValue = int.Parse(hexString.ToString(),
NumberStyles.AllowHexSpecific);

I am getting a positive intValue. However, the intValue is supposed to
be a negative value. Any help would be greatly appreciated.

JT


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top