将byte []转换为signed int [英] Converting byte[] to signed int

查看:110
本文介绍了将byte []转换为signed int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将byte []转换为signed int时需要一些帮助。这就是我试图做的事情:


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);


我得到一个正的intValue。但是,intValue应该是
为负值。任何帮助将不胜感激。


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

推荐答案

我看不出为什么你期待负面价值。

[BASE16] FFFF9C = [BASE10] 16777116

-

问候,

DennisJDMyrén

Oslo Kodebureau

" JT" < jt@jt.jt>在消息中写道

新闻:OF ************** @ TK2MSFTNGP09.phx.gbl ...
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...
在转换中需要一些帮助一个byte []到一个signed int。这就是我试图做的事情:

byte [] bytes = new byte [3] {0xFF,0xFF,0x9C};
StringBuilder hexString = new StringBuilder() ; foreach(byte tmpByte,以字节为单位)hexString.Append(tmpByte.ToString(" X2"));

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

我得到一个积极的intValue。但是,intValue应该是负值。任何帮助将不胜感激。

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



对不起,我应该更多地解释我的情况。我从连接到串行端口的设备获得了一些

字节。设备显示一个

负值,我得到的值为FFFF9C。

此外,API文档说明字节是Big-Endian。在

这种情况​​下,FFFF9C会转换为某个负整数值吗?


DennisMyrén写道:
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无法理解为什么你期望负值。
[BASE16] FFFF9C = [BASE10] 16777116
I cannot see why you are expecting a negative value.
[BASE16] FFFF9C = [BASE10] 16777116



JT< jt@jt.jt> ;写道:
JT <jt@jt.jt> wrote:
在将byte []转换为signed int时需要一些帮助。这就是我试图做的事情:

byte [] bytes = new byte [3] {0xFF,0xFF,0x9C};
StringBuilder hexString = new StringBuilder() ; foreach(byte tmpByte,以字节为单位)hexString.Append(tmpByte.ToString(" X2"));

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

我得到一个积极的intValue。但是,intValue应该是负值。任何帮助将不胜感激。
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.




24位签名int是非常罕见的 - 你真的是指它只有

3字节而不是4?


您可以使用BitConverter进行转换,而无需使用字符串

格式。如果这是错误的字节顺序,你可以使用我的

EndianBitConverter来自
http://www.pobox.com/~skeet/csharp/miscutil


-

Jon Skeet - < sk *** @ pobox.com>
http ://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



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


这篇关于将byte []转换为signed int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆