gpt4 book ai didi

c# - 如何从 byte[] 中读取整数

转载 作者:太空狗 更新时间:2023-10-29 18:05:34 24 4
gpt4 key购买 nike

我有一个字节数组,我想从这个数组中读取一个整数。我该怎么做?

像这样:

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

等...

最佳答案

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

引用:How to: Convert a byte Array to an int

关于c# - 如何从 byte[] 中读取整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6370122/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com