如何从一个字节读整数[]整数、字节

2023-09-03 06:52:14 作者:要么拼命爱 要么趁早滚

我有一个字节数组,我想读此数组的整数。我该怎么办呢?

事情是这样的:

  INT I;

 选项​​卡=新的字节[32];

 我=的readInt(标签,0,3); //我= INT从标签[0]到标签[3](INT = 4个字节?)

 我=的readInt(标签,4,7);
 

等等...

解决方案

 字节[]字节= {0,0,0,25};

//如果系统架构是小端(也就是,小端的第一),
//反转的字节数组。
如果(BitConverter.IsLittleEndian)
    Array.Reverse(字节);

INT I = BitConverter.ToInt32(字节,0);
Console.WriteLine(INT:{0},我);
 

参考:如何:将一个字节数组为int

一个整数占几个字节

I have a byte array, and I would like to read an integer from this array. How can I do it?

Something like this:

 int i;

 tab = new byte[32];

 i = readint(tab,0,3); // i = int from tab[0] to tab[3] (int = 4 bytes?)

 i = readint(tab,4,7);

etc...

解决方案

byte[] bytes = { 0, 0, 0, 25 };

// If the system architecture is little-endian (that is, little end first),
// reverse the byte array.
if (BitConverter.IsLittleEndian)
    Array.Reverse(bytes);

int i = BitConverter.ToInt32(bytes, 0);
Console.WriteLine("int: {0}", i);

Ref: How to: Convert a byte Array to an int

 
精彩推荐
图片推荐