java 二进制字符串转十六进制字符串
我这里有这段代码,它获取纯文本,并将其转换为 512 位二进制字符串。 然后,我想将字符串的每个 32 位片段转换为 8 位十六进制字符串,但这部分给了我一个 java.lang.NumberFormatException
// ----- Turning the message to bits
byte[] binaryS = s.getBytes("UTF-8");
String mesInBinary = "";
for (byte b : binaryS) {
mesInBinary += '0' + Integer.toBinaryString(b);
}
// ----- Message padding & Pre-Processing
// Binary representation of the length of the message in bits
String mesBitLength = Integer.toBinaryString(mesInBinary.length());
// We need the size of the message in 64-bits, so we'll
// append zeros to the binary length of the message so
// we get 64-bit
String appendedZeros = "";
for (int i = 64 - mesBitLength.length() ; i > 0 ; i--)
appendedZeros += '0';
// Calculating the k zeros to append to the message after
// the appended '1'
int numberOfZeros = (448 - (mesInBinary.length() + 1)) % 512;
// Append '1' to the message
mesInBinary += '1';
// We need a positive k
while (numberOfZeros < 0)
numberOfZeros += 512;
for (int i = 1 ; i <= numberOfZeros ; i++)
mesInBinary += '0';
// append the message length in 64-bit format
mesInBinary += appendedZeros + mesBitLength;
System.out.println(mesInBinary);
// ----- Parsing the padded message
// Breaking the message to 512-bit pieces
// And each piece, to 16 32-bit word blocks
String[] chunks = new String[mesInBinary.length() / 512];
String[] words = new String[64 * chunks.length];
for (int i = 0 ; i < chunks.length ; i++) {
chunks[i] = mesInBinary.substring((512 * i), (512 * (i + 1)));
// Break each chunk to 16 32-bit blocks
for (int j = 0 ; j < 16 ; j++) {
words[j] = Long.toHexString(Long.parseLong(chunks[i].substring((32 * j), (32 * (j + 1)))));
}
}
最后一行代码是有问题的,我得到了其中的例外。有什么建议吗?
I have this code here, which get a plain text, and turns it to a 512-bit binary string.
Then, I would like to turn each 32-bit piece of the string to 8-bit of Hex string, but that part gives me a java.lang.NumberFormatException
// ----- Turning the message to bits
byte[] binaryS = s.getBytes("UTF-8");
String mesInBinary = "";
for (byte b : binaryS) {
mesInBinary += '0' + Integer.toBinaryString(b);
}
// ----- Message padding & Pre-Processing
// Binary representation of the length of the message in bits
String mesBitLength = Integer.toBinaryString(mesInBinary.length());
// We need the size of the message in 64-bits, so we'll
// append zeros to the binary length of the message so
// we get 64-bit
String appendedZeros = "";
for (int i = 64 - mesBitLength.length() ; i > 0 ; i--)
appendedZeros += '0';
// Calculating the k zeros to append to the message after
// the appended '1'
int numberOfZeros = (448 - (mesInBinary.length() + 1)) % 512;
// Append '1' to the message
mesInBinary += '1';
// We need a positive k
while (numberOfZeros < 0)
numberOfZeros += 512;
for (int i = 1 ; i <= numberOfZeros ; i++)
mesInBinary += '0';
// append the message length in 64-bit format
mesInBinary += appendedZeros + mesBitLength;
System.out.println(mesInBinary);
// ----- Parsing the padded message
// Breaking the message to 512-bit pieces
// And each piece, to 16 32-bit word blocks
String[] chunks = new String[mesInBinary.length() / 512];
String[] words = new String[64 * chunks.length];
for (int i = 0 ; i < chunks.length ; i++) {
chunks[i] = mesInBinary.substring((512 * i), (512 * (i + 1)));
// Break each chunk to 16 32-bit blocks
for (int j = 0 ; j < 16 ; j++) {
words[j] = Long.toHexString(Long.parseLong(chunks[i].substring((32 * j), (32 * (j + 1)))));
}
}
The last code line is the problematic one and of which I get the execption. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最后一个语句*应该指定基数2,我认为:
*不是最后一行代码,MДДГИ:-)
The last statement* should specify a radix of 2, I think:
*Not the last line of code, MДΓΓ :-)
来自
Long
文档:public static long parseLong(String s) 抛出 NumberFormatException
:public static long parseLong(String s, int radix) throws NumberFormatException
:您正在调用
Long.parseLong()
的第一个版本,它需要一个十进制数字,不是二进制的。使用基数为 2 的第二个版本来表示二进制。编辑:原因是 32 位十进制数字不适合
Long
,但二进制数字可以。From the
Long
docs:public static long parseLong(String s) throws NumberFormatException
:public static long parseLong(String s, int radix) throws NumberFormatException
:You're calling the first version of
Long.parseLong()
, which expects a decimal number, not a binary one. Use the second version with a radix of 2 to indicate binary.EDIT: The reason being that a 32-digit decimal number won't fit into a
Long
, but a binary one will.