-3
我试图将160长度的二进制字符串转换为20个字节。我正在使用BigInteger来获取一个字节数组。它只返回14个字节,我需要20个字节。使用BigInteger获取字节数组给予二进制字符串,返回的字节数组dosen的包含0值字节
这里是我的代码:
BigInteger b = new BigInteger("0000000000000000000000000000000000000000000001010000000000000000000000000000010000010100001011111110000000000000000000000000000000000000000000000000000000000000", 2);
byte[] newData = b.toByteArray();
ByteArrayOutputStream output = new ByteArrayOutputStream(20);
for (int i = 0; i < 20 - newData.length; i++) {
output.write((byte) 0x00);
}
output.write(newData);
newData = output.toByteArray();
这样做的目的是什么?你可以正常创建一个字节数组,尽管你认为我确信bigInteger正在做转换正确 – aaronman
你甚至读过'toByteArray()'的文档吗? “该数组将包含表示此BigInteger所需的最小字节数”。它修剪了前面的零,毕竟它期望处理数字。 –
为什么你试图在160字节中填入8个字节? – user2357112