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 10 11338
I cannot see why you are expecting a negative value.
[BASE16] FFFF9C = [BASE10] 16777116
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"JT" <jt@jt.jt> wrote in message
news:OF**************@TK2MSFTNGP09.phx.gbl... 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
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?
Dennis Myrén wrote: I cannot see why you are expecting a negative value. [BASE16] FFFF9C = [BASE10] 16777116
JT <jt@jt.jt> wrote: 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
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Then, try:
int result = (a [0x2] << 0x8) + (a [0x1] << 0x10) + (a [0x0] << 0x18);
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"JT" <jt@jt.jt> wrote in message
news:uy****************@TK2MSFTNGP15.phx.gbl... 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?
Dennis Myrén wrote:
I cannot see why you are expecting a negative value. [BASE16] FFFF9C = [BASE10] 16777116
(Where 'a' is represents your variable 'bytes')
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:lf********************@news4.e.nsc.no... Then, try: int result = (a [0x2] << 0x8) + (a [0x1] << 0x10) + (a [0x0] << 0x18);
-- Regards, Dennis JD Myrén Oslo Kodebureau "JT" <jt@jt.jt> wrote in message news:uy****************@TK2MSFTNGP15.phx.gbl... 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?
Dennis Myrén wrote:
I cannot see why you are expecting a negative value. [BASE16] FFFF9C = [BASE10] 16777116
JT wrote: In this case, would FFFF9C translate to some negative integer value?
Yes, it would be -25600 .
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"JT" <jt@jt.jt> wrote in message
news:uy****************@TK2MSFTNGP15.phx.gbl... 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?
Dennis Myrén wrote:
I cannot see why you are expecting a negative value. [BASE16] FFFF9C = [BASE10] 16777116
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" <jt@jt.jt> wrote in message
news:uy****************@TK2MSFTNGP15.phx.gbl... 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?
Dennis Myrén wrote:
I cannot see why you are expecting a negative value. [BASE16] FFFF9C = [BASE10] 16777116
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" <jt@jt.jt> wrote in message
news:OF**************@TK2MSFTNGP09.phx.gbl... 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
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 intothe 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 shouldbe 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" <jt@jt.jt> wrote in message news:OF**************@TK2MSFTNGP09.phx.gbl... 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
.
"JT" <an*******@discussions.microsoft.com> wrote in message
news:7a****************************@phx.gbl... 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 intothe 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 shouldbe 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" <jt@jt.jt> wrote in message news:OF**************@TK2MSFTNGP09.phx.gbl... 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
.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Hal Vaughan |
last post by:
If I have a byte and I convert it to string (String sData = new
String(byte bData), then convert it back (byte bData = sData.getBytes()),
will all data be intact, or do Strings have problems with...
|
by: w3r3w0lf |
last post by:
hello!
I have a following situation:
I have a byte array where at a certain location are stored 4 bytes, and
these should be "put" into long variable (or any other 4 byte one).
ie:
byte...
|
by: cpptutor2000 |
last post by:
Could some C guru help me please? A byte is an unsigned char in C.
How do I convert from a C string to a corresponding byte array. Any
help would be greatly appreciated.
|
by: _Andy_ |
last post by:
I'm looking for the alogithm to take a piece of 8-bit audio data, and
scale it to a 16-bit short. I got as far as:
private static short ByteToShort(byte b)
{
int word = 0;
word |= ( ( b &...
|
by: msosno01 |
last post by:
I have Java client that connects to C++ server. The client sends
integer in binary using DataOutputStream write function. I am reading
these data into buffer. I have to convert this buffer back...
| |
by: Manuel |
last post by:
hi,
I have a problem, a stupid problem. I can't declare a variable of type
byte.
The g++ said that i have syntactic error in this line. The code is
this: byte * variable;
well, i think...
|
by: John Fisher |
last post by:
Hi Group,
troubles with converting signed 32.32, little-endian, 2's complement
back to floating point. I have been trying to brew it myself. I am
running Python 2.5 on a Mac. Here is the C-code...
|
by: Lamefif |
last post by:
how can the computer tell the difference between the two?
i mean a byte is 8 bit can be 1 or 0
11111111 = 255 unsigned byte
10000000 = -128 or 128 ?
|
by: itdevries |
last post by:
Hi,
I'm trying to read some binary data from a file, I've read a few bytes
of the data into a
char array with ifstream. Now I know that the first 4 bytes in the
char array represent
an integer....
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |