byte[] 到无符号 BigInteger?
动机: 我想将哈希值(MD5/SHA1 等)转换为十进制整数,以便在 Code128C 中制作条形码。 为简单起见,我希望所有结果(大)数字都是正数。
我能够在 C# 中将 byte[] 转换为 BigInteger...
到目前为止我所拥有的示例:
byte[] data;
byte[] result;
BigInteger biResult;
result = shaM.ComputeHash(data);
biResult = new BigInteger(result);
但是(这里生锈的CS)我是否正确,字节数组总是可以用两种方式解释:
- (A):作为有符号数
- (B):作为无符号数
是吗?是否可以在 C# 中从 byte[] 生成 UNSIGNED BigInteger?
我应该简单地在 byte[] 前面添加 0x00(零字节)吗?
编辑: 感谢 AakashM、Jon 和 Adam Robinson,附加一个零字节实现了我所需要的。
编辑2: 我应该做的主要事情是阅读 BigInteger(byte[]) 构造函数的详细文档,然后我会看到有关如何通过附加零字节来限制正数的部分。
Motivation:
I would like to convert hashes (MD5/SHA1 etc) into decimal integers for the purpose of making barcodes in Code128C.
For simplicity, I prefer all the resulting (large) numbers to be positive.
I am able to convert byte[] to BigInteger in C#...
Sample from what I have so far:
byte[] data;
byte[] result;
BigInteger biResult;
result = shaM.ComputeHash(data);
biResult = new BigInteger(result);
But (rusty CS here) am I correct that a byte array can always be interpreted in two ways:
- (A): as a signed number
- (B): as an unsigned number
Is it possible to make an UNSIGNED BigInteger from a byte[] in C#?
Should I simply prepend a 0x00 (zero byte) to the front of the byte[]?
EDIT:
Thank you to AakashM, Jon and Adam Robinson, appending a zero byte achieved what I needed.
EDIT2:
The main thing I should have done was to read the detailed doc of the BigInteger(byte[]) constructor, then I would have seen the sections about how to restrict to positive numbers by appending the zero byte.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
BigInteger 的备注< /code> 构造函数 声明您可以确保从 a 创建的任何
BigInteger
如果在调用构造函数之前将00
字节附加到数组末尾,则byte[]
是无符号的。注意:
BigInteger
构造函数要求数组采用小端顺序。如果您希望生成的 BigInteger 具有特定值,请记住这一点。The remarks for the
BigInteger
constructor state that you can make sure anyBigInteger
created from abyte[]
is unsigned if you append a00
byte to the end of the array before calling the constructor.Note: the
BigInteger
constructor expects the array to be in little-endian order. Keep that in mind if you expect the resultingBigInteger
to have a particular value.从 .NET Core 2.1 开始,
BigInteger
具有 构造函数:Since .NET Core 2.1,
BigInteger
has a constructor with an optional parameterisUnsigned
:检查相关
BigInteger
构造函数的文档 ,我们看到:[...]
[...]
Examining the documentation for the relevant
BigInteger
constructor, we see:[...]
[...]
正如其他答案所指出的,您应该在数组末尾附加一个 00 字节,以确保生成的 BigInteger 为正。
根据 BigInteger 结构 (System.Numerics) MSDN 文档
下面是执行此操作的代码:
As other answers have pointed out, you should append a 00 byte to the end of the array to ensure the resulting BigInteger is positive.
According to the the BigInteger Structure (System.Numerics) MSDN Documentation
Here's code to do it:
更正确的是所有数字(由于存储在计算机中)基本上是一系列字节,这就是字节数组。字节数组始终可以解释为特定数字类型的有符号或无符号版本是不正确的,因为并非所有数字类型都有有符号和无符号版本。浮点类型通常只有有符号版本(没有
udouble
或ufloat
),并且在本例中,没有无符号版本的BigInteger
>。因此,换句话说,不,这是不可能的,但由于 BigInteger 可以表示任意大的整数值,因此您不会因为它的签名而丢失任何范围。
至于第二个问题,您需要将
0x00
附加到数组的末尾 end,如BigInteger
构造函数 以小端字节顺序解析值。What's more correct is that all numbers (by virtue of being stored in the computer) are basically a series of bytes, which is what a byte array is. It's not true to say that a byte array can always be interpreted as a signed or unsigned version of a particular numeric type, as not all numeric types have signed and unsigned versions. Floating point types generally only have signed versions (there's no
udouble
orufloat
), and, in this particular instance, there is no unsigned version ofBigInteger
.So, in other words, no, it's not possible, but since
BigInteger
can represent an arbitrarily large integer value, you're not losing any range by virtue of its being signed.As to your second question, you would need to append
0x00
to end end of the array, as theBigInteger
constructor parses the values in little-endian byte order.