字节[]为无符号的BigInteger? [英] byte[] to unsigned BigInteger?
问题描述
动机:
我想哈希(MD5 / SHA1等)转换成十进制整数为code128C使得酒吧codeS的目的。
为了简单起见,我preFER全部所得的(大)数为阳性
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.
我能够字节[]转换为BigInteger的在C#...结果
从我至今样品:
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);
但(CS生锈这里)我是纠正一个字节数组总是可以通过两种方式PTED间$ P $:
答:作为一个符号数
A:作为一个无符号数
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
是否有可能从C#中的字节[]使一个无符号的BigInteger?
我应该只是prePEND一个为0x00(零字节)的字节前面[]?
Should I simply prepend a 0x00 (zero byte) to the front of the byte[]?
编辑:
谢谢AakashM,乔恩和亚当·罗宾逊,追加一个零字节来实现我所需要的。\\
Thank you to AakashM, Jon and Adam Robinson, appending a zero byte achieved what I needed.\
EDIT2:
我应该做的主要事情是阅读的BigInteger(字节[])构造的详细文档,那么我会看到有关如何通过追加零字节限制到正数的章节。
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.
推荐答案
的<一个href=\"http://msdn.microsoft.com/en-us/library/dd268207.aspx#d04b3694-ade4-4482-99f3-267d0862569c\">remarks为的BigInteger
构造状态,你可以确保任何从的BigInteger
>字节[] 是无符号的,如果你在调用构造函数之前追加一个 00
字节以数组的结尾。
The remarks for the BigInteger
constructor state that you can make sure any BigInteger
created from a byte[]
is unsigned if you append a 00
byte to the end of the array before calling the constructor.
注:的BigInteger
构造函数需要数组在little-endian顺序。记住这一点,如果你希望得到的的BigInteger
有一个特定的值。
Note: the BigInteger
constructor expects the array to be in little-endian order. Keep that in mind if you expect the resulting BigInteger
to have a particular value.
这篇关于字节[]为无符号的BigInteger?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!