gpt4 book ai didi

c# - 将零字节添加到字节数组的最简单方法是什么?

转载 作者:行者123 更新时间:2023-11-30 13:23:25 25 4
gpt4 key购买 nike

我正在将 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);
}

Thanks dtb

最佳答案

试试这个:

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/

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