如何从一个字节读整数[] [英] How to read an integer from a byte[]
本文介绍了如何从一个字节读整数[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字节数组,我想读此数组的整数。我该怎么办呢?
事情是这样的:
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},我);
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
这篇关于如何从一个字节读整数[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文