将字节数组转换为 BigInteger
Convert byte array to BigInteger
我的数字从左到右以 256 为基数,并以字节数组表示。我想将它们转换为 BigInteger,这样下面的例子就可以工作了:
- [5] -> 5
- [200] -> 200
- [0,1] -> 256
- [100,2] -> 612
我想到了这个解决方案:
byte[] input = new byte[]{(byte) 200,2};
BigInteger a = BigInteger.ZERO;
BigInteger base = BigInteger.valueOf(256);
for (int i = 0; i < input.length; i++) {
a = a.add(BigInteger.valueOf(input[i] & 0xFF).multiply(base.pow(i)));
}
System.out.println(a);
虽然有效,但感觉效率很低。有更有效的方法吗?
我知道你能做到:
import java.math.BigInteger;
import java.util.BitSet;
public class Main {
public static void main(String[] argv) throws Exception {
// A negative value
byte[] bytes = new byte[] { (byte) 0xFF, 0x00, 0x00 }; // -65536
// A positive value
bytes = new byte[] { 0x1, 0x00, 0x00 }; // 65536
BitSet set = BitSet.valueOf(bytes);
set.flip(0, set.length());
byte[] flipped = set.toByteArray();
BigInteger bi = new BigInteger(flipped);
}
}
我将 BitSet 用于 swat 位,因为你想从左到右,但 BigInteger 构造函数使用从右到左
从字节数组创建 BigInteger
的最简单方法是使用 new 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.
由于您的输入数组是 little-endian 顺序,并且您不希望返回负数,因此您需要反转字节并确保第一个字节为 0- 127,所以没有设置符号位。最简单的方法是使第一个字节 0
.
示例:[2, 20, 200]
-> [0, 200, 20, 2]
这是相关代码:
private static BigInteger toBigInt(byte[] arr) {
byte[] rev = new byte[arr.length + 1];
for (int i = 0, j = arr.length; j > 0; i++, j--)
rev[j] = arr[i];
return new BigInteger(rev);
}
测试
byte[][] data = { {5},
{(byte)200},
{0,1},
{100,2} };
for (byte[] arr : data)
System.out.println(toBigInt(arr));
输出
5
200
256
612
我的数字从左到右以 256 为基数,并以字节数组表示。我想将它们转换为 BigInteger,这样下面的例子就可以工作了:
- [5] -> 5
- [200] -> 200
- [0,1] -> 256
- [100,2] -> 612
我想到了这个解决方案:
byte[] input = new byte[]{(byte) 200,2};
BigInteger a = BigInteger.ZERO;
BigInteger base = BigInteger.valueOf(256);
for (int i = 0; i < input.length; i++) {
a = a.add(BigInteger.valueOf(input[i] & 0xFF).multiply(base.pow(i)));
}
System.out.println(a);
虽然有效,但感觉效率很低。有更有效的方法吗?
我知道你能做到:
import java.math.BigInteger;
import java.util.BitSet;
public class Main {
public static void main(String[] argv) throws Exception {
// A negative value
byte[] bytes = new byte[] { (byte) 0xFF, 0x00, 0x00 }; // -65536
// A positive value
bytes = new byte[] { 0x1, 0x00, 0x00 }; // 65536
BitSet set = BitSet.valueOf(bytes);
set.flip(0, set.length());
byte[] flipped = set.toByteArray();
BigInteger bi = new BigInteger(flipped);
}
}
我将 BitSet 用于 swat 位,因为你想从左到右,但 BigInteger 构造函数使用从右到左
从字节数组创建 BigInteger
的最简单方法是使用 new 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.
由于您的输入数组是 little-endian 顺序,并且您不希望返回负数,因此您需要反转字节并确保第一个字节为 0- 127,所以没有设置符号位。最简单的方法是使第一个字节 0
.
示例:[2, 20, 200]
-> [0, 200, 20, 2]
这是相关代码:
private static BigInteger toBigInt(byte[] arr) {
byte[] rev = new byte[arr.length + 1];
for (int i = 0, j = arr.length; j > 0; i++, j--)
rev[j] = arr[i];
return new BigInteger(rev);
}
测试
byte[][] data = { {5},
{(byte)200},
{0,1},
{100,2} };
for (byte[] arr : data)
System.out.println(toBigInt(arr));
输出
5
200
256
612