Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将字节解释为int?_C#_.net 3.5_Compact Framework_Bytebuffer_Uint8t - Fatal编程技术网

C# 如何将字节解释为int?

C# 如何将字节解释为int?,c#,.net-3.5,compact-framework,bytebuffer,uint8t,C#,.net 3.5,Compact Framework,Bytebuffer,Uint8t,我有一个从Cpp程序接收到的字节数组 arr[0..3] // a real32, arr[4] // a uint8, 如何将arr[4]解释为int (uint)arr[4] // Err: can't implicitly convert string to int. BitConverter.ToUint16(arr[4]) // Err: Invalid argument. buff[0+4] as int // Err: must be reference or nullab

我有一个从Cpp程序接收到的字节数组

arr[0..3] // a real32,
arr[4]    // a uint8,
如何将
arr[4]
解释为
int

(uint)arr[4] // Err: can't implicitly convert string to int.
BitConverter.ToUint16(arr[4]) // Err: Invalid argument.
buff[0+4] as int // Err: must be reference or nullable type
我是否必须将连续字节归零才能将其解释为
UInt16

好的,这里是混乱。最初,我定义了我的类

byte[] buff;
buff = getSerialBuffer();

public class Reading{
    public string scale_id;
    public string measure;
    public int measure_revised;
    public float wt;
}
rd = new Reading();
// !! here is the confusion... !!
// Err: Can't implicitly convert 'string' to 'int'
rd.measure = string.Format("{0}", buff[0 + 4]); 
// then I thought, maybe I should convert buff[4] to int first ? 
// I throw all forms of conversion here, non worked.
// but, later it turns out:
rd.measure_revised = buff[0+4]; // just ok.
所以基本上,我不明白为什么会这样

rd.measure = string.Format("{0}", buff[0 + 4]); 
//Err: Can't implicitly convert 'string' to 'int'

如果buff[4]是一个字节,而byte是uint8,那么
不能将字符串隐式转换为int是什么意思呢?。。。这让我很困惑。

你就快到了。假设您需要前4个字节中的32位int(很难解释您的问题):

这表示从索引
0
开始,从
arr
获取4个字节,并将它们转换为32位整数()

请注意,
BitConverter
使用计算机的尾数,因此在x86/x64上这将是小尾数

如果要使用显式endianness,则需要手动构造int:

int littleEndian = arr[0] | (arr[1] << 8) | (arr[2] << 16) | (arr[3] << 24);
int bigEndian = arr[3] | (arr[2] << 8) | (arr[1] << 16) | (arr[0] << 24);

int littleEndian=arr[0]|(arr[1]如果我没听错的话,你有
byte
(而不是
string
)数组

要返回
float
byte
,您可以尝试

结果:

  Real Part: 1.234; Integer Part: 123
  182 243 157 63 123
如果我们想编码
arr
,同样的想法(
BitConverter
class):

  float realPart = 1.234f;
  byte bytePart = 123;

  byte[] arr = 
     BitConverter.GetBytes(realPart)
    .Concat(new byte[] { bytePart })
    .ToArray();

  Console.Write(string.Join(" ", arr));
结果:

  Real Part: 1.234; Integer Part: 123
  182 243 157 63 123

(uint)arr[4]//错误:无法将字符串隐式转换为int。
这似乎很奇怪……arr
的类型是什么?您似乎对是要32位int还是16位int感到困惑。
int
uint
Int32
UInt32
)是4字节(32位).
short
ushort
Int16
UInt16
)是2个字节(16位)。对不起,它的arr是一个字节数组。我想要一个uin8,它在C#中不存在……所以我只想arr[4]用作int。
uint8
存在并被调用为
byte
。您可以将
byte
强制转换为
int
/
uint
,没有任何问题。因此
(uint)arr[4]//错误:无法隐式地将字符串转换为int。
将起作用。如果
arr
字节[/code>,则第一次转换
(uint)arr[4]
将起作用。(在不会导致给定错误消息的意义上起作用)
  182 243 157 63 123