【发布时间】:2020-10-04 18:02:47
【问题描述】:
我有这个 python 代码:
f = open('file.bin', 'rb')
b = f.read(2)
bytes = b[0x0:0x2] //this is b'\x10\x24', for example
f.close()
a = int.from_bytes(bytes, 'big') //returns 4132
我似乎无法弄清楚如何在 C# 中实现同样的功能。
确实找到了这个方法:
public static int IntFromBigEndianBytes(byte[] data, int startIndex = 0)
{
return (data[startIndex] << 24) | (data[startIndex + 1] << 16) | (data[startIndex + 2] << 8) | data[startIndex + 3];
}
尝试该方法总是会导致 IndexOutOfRangeException,因为我的输入字节小于 4。了解为什么会发生这种情况,否则任何信息将不胜感激。
【问题讨论】:
标签: python c# endianness