我正在将 byte[]
转换为 BigInteger
,我想确保它的结果是正数。 The docs say :
To prevent positive values from being misinterpreted as negative values, you can add a zero-byte value to the end of the array.
但它没有具体说明如何。那么,我该怎么做呢?
发现这是最简单的:
public static BigInteger UnsignedBigInt(byte[] bytes)
{
if ((bytes[bytes.Length - 1] & 0x80) != 0) Array.Resize(ref bytes, bytes.Length + 1);
return new BigInteger(bytes);
}
最佳答案
试试这个:
byte[] bytes = ...
if ((bytes[bytes.Length - 1] & 0x80) != 0)
{
Array.Resize<byte>(ref bytes, bytes.Length + 1);
}
BigInteger result = new BigInteger(bytes);
关于c# - 将零字节添加到字节数组的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12292311/