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

标签 c#

我正在将 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/

相关文章:

c# - 对话框在 wpf 弹出窗口后面打开

c# - 如何在不在每个过程中创建附加参数的情况下将线程文化从应用程序传递到 sql server?

c# - 基于 MEF 的应用程序在本地计算机上运行良好,但在从网络共享运行时不会导入加载项

c# - Mono Assembly 未解决

c# - 将日期格式更改为 ddth mmm,yyyy

c# - 如何解决这个错误 "HTTP Error 500.19 - Internal Server Error"

c# - 哪一种是在 SQL Server 中复制数据库的最佳方法?

c# - 使用 Database.SqlQuery() 的 Entity Framework ,其中列名是无效的 C# 成员名称

c# - 如何直接从 C# 服务器控制台监控 HTTP 请求?

c# - 在运行时向现有的 c# 类添加属性