如何将我的字符串(代表十六进制值)转换为字节?
how can I convert my String (that represents hex values) to bytes?
我在 Java
中有一个包含 32 个字符的字符串:
String tempHash = "123456789ABCDEF123456789ABCDEF12";
上面字符串中的每个字符代表一个十六进制值。我需要将它转换为另一个字符串,该字符串包含由上述字符串的每个十六进制计算的 8 字节。所以在上面的例子中,输出字符串将是:
"00000001 00000010 00000011 000001000 000001001 000001011 ..."
我该怎么做?
我试过:
byte[] bytes1 = toByteArray(tempHash);
其中
public static byte[] toByteArray(String s) {
return DatatypeConverter.parseHexBinary(s);
}
但是当我遍历这个数组时:
for (byte singleByte: bytes1) {
System.out.println(singleByte);
}
对于第一个字符,我得到 18
而不是 00000001
。
我在这里有点迷路了。你能帮我吗?
public byte hexToByte(String hexString) {
int firstDigit = toDigit(hexString.charAt(0));
int secondDigit = toDigit(hexString.charAt(1));
return (byte) ((firstDigit << 4) + secondDigit);
}
private int toDigit(char hexChar) {
int digit = Character.digit(hexChar, 16);
if(digit == -1) {
throw new IllegalArgumentException(
"Invalid Hexadecimal Character: "+ hexChar);
}
return digit;
}
Here为参考
您可以使用Long.parseLong(String,16);
一旦你有一个long
值,你可以通过
获取字节
long val = ...;
ByteBuffer buf = new ByteBuffer();
buf.put(0, val);
如果您的字符串太长,您将需要使用 BigInteger。本质上是一样的,只是稍微复杂一点
一个解决方案是使用 Stream
:
String tempHash = "123456789ABCDEF123456789ABCDEF12";
String binary = tempHash.chars() // Get stream of chars
.map(c -> Character.digit(c, 16)) // Convert to hex digit
.mapToObj(Integer::toBinaryString) // Convert to binary
.map(s -> "0".repeat(8 - s.length()) + s) // Pad left with zeros
.collect(Collectors.joining(" ")); // Collect to String
System.out.println(binary);
输出:
00000001 00000010 00000011 00000100 00000101 ...
如Kevin pointed out in below, a pre-Java 11 solution would be to replace the call to String#repeat
:
String binary = tempHash.chars() // Get stream of chars
.map(c -> Character.digit(c, 16)) // Convert to hex digit
.mapToObj(Integer::toBinaryString) // Convert to binary
.map(s -> new String(new char[8 - s.length()]).replace('[=12=]', '0') + s) // Pad left with zeros
.collect(Collectors.joining(" ")); // Collect to String
我在 Java
中有一个包含 32 个字符的字符串:
String tempHash = "123456789ABCDEF123456789ABCDEF12";
上面字符串中的每个字符代表一个十六进制值。我需要将它转换为另一个字符串,该字符串包含由上述字符串的每个十六进制计算的 8 字节。所以在上面的例子中,输出字符串将是:
"00000001 00000010 00000011 000001000 000001001 000001011 ..."
我该怎么做?
我试过:
byte[] bytes1 = toByteArray(tempHash);
其中
public static byte[] toByteArray(String s) {
return DatatypeConverter.parseHexBinary(s);
}
但是当我遍历这个数组时:
for (byte singleByte: bytes1) {
System.out.println(singleByte);
}
对于第一个字符,我得到 18
而不是 00000001
。
我在这里有点迷路了。你能帮我吗?
public byte hexToByte(String hexString) {
int firstDigit = toDigit(hexString.charAt(0));
int secondDigit = toDigit(hexString.charAt(1));
return (byte) ((firstDigit << 4) + secondDigit);
}
private int toDigit(char hexChar) {
int digit = Character.digit(hexChar, 16);
if(digit == -1) {
throw new IllegalArgumentException(
"Invalid Hexadecimal Character: "+ hexChar);
}
return digit;
}
Here为参考
您可以使用Long.parseLong(String,16);
一旦你有一个long
值,你可以通过
long val = ...;
ByteBuffer buf = new ByteBuffer();
buf.put(0, val);
如果您的字符串太长,您将需要使用 BigInteger。本质上是一样的,只是稍微复杂一点
一个解决方案是使用 Stream
:
String tempHash = "123456789ABCDEF123456789ABCDEF12";
String binary = tempHash.chars() // Get stream of chars
.map(c -> Character.digit(c, 16)) // Convert to hex digit
.mapToObj(Integer::toBinaryString) // Convert to binary
.map(s -> "0".repeat(8 - s.length()) + s) // Pad left with zeros
.collect(Collectors.joining(" ")); // Collect to String
System.out.println(binary);
输出:
00000001 00000010 00000011 00000100 00000101 ...
如Kevin pointed out in String#repeat
:
String binary = tempHash.chars() // Get stream of chars
.map(c -> Character.digit(c, 16)) // Convert to hex digit
.mapToObj(Integer::toBinaryString) // Convert to binary
.map(s -> new String(new char[8 - s.length()]).replace('[=12=]', '0') + s) // Pad left with zeros
.collect(Collectors.joining(" ")); // Collect to String