byte[] byteArray = {
52, 51, 102, 100, 55, 48, 48, 48, 57, 97, 57, 55, 97, 55, 100, 51, 49, 49, 99, 53, 54, 52, 52,
48, 52, 55, 99, 99, 99, 55, 48, 48, 102, 56, 100, 48, 56, 97, 57, 100
};
BigInteger byteArrayAsBigInt = new BigInteger(byteArray);
正如您所看到的,两个阵列都是相同的.但是为什么Java中的BigInteger构造函数返回的值不同于C#中的值?
byteArrayAsBigInt的Java值:
435547623972009042387221878687981899647773248766318257271173050301525056529400623692496442046820
byteArrayAsBigInt的C#值:
836240090191738952707023426454050812020217962491637996803829702297801636146665723913243623568180
任何人都有任何想法?
解决方法:
答案是在Java Big Endian中假设顺序,但在C#中使用Little Endian.
从Java文档:
public BigInteger(byte[] val) Translates a byte array containing the
two’s-complement binary representation of a BigInteger into a
BigInteger. The input array is assumed to be in big-endian byte-order:
the most significant byte is in the zeroth element.
来自C#文档:
BigInteger Constructor (Byte[])
Type: System.Byte[]
An array of byte values in little-endian order.
标签:c,java
来源: https://codeday.me/bug/20190708/1402419.html