Java 十六进制编码的字符串到字节数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6650650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hex-encoded String to Byte Array
提问by Qaiser Mehmood
String str = "9B7D2C34A366BF890C730641E6CECF6F";
I want to convert str
into byte array, but str.getBytes()
returns 32 bytes instead of 16.
我想转换str
成字节数组,但str.getBytes()
返回 32 个字节而不是 16 个字节。
采纳答案by pap
I think what the questioner is after is converting the string representation of a hexadecimal value to a byte array representing that hexadecimal value.
我认为提问者所追求的是将十六进制值的字符串表示形式转换为表示该十六进制值的字节数组。
The apache commons-codec has a class for that, Hex.
apache commons-codec 有一个类,Hex。
String s = "9B7D2C34A366BF890C730641E6CECF6F";
byte[] bytes = Hex.decodeHex(s.toCharArray());
回答by Eng.Fouad
Use:
用:
str.getBytes("UTF-16LE");
回答by Jean Logeart
That should do the trick :
这应该够了吧 :
byte[] bytes = toByteArray(Str.toCharArray());
public static byte[] toByteArray(char[] array) {
return toByteArray(array, Charset.defaultCharset());
}
public static byte[] toByteArray(char[] array, Charset charset) {
CharBuffer cbuf = CharBuffer.wrap(array);
ByteBuffer bbuf = charset.encode(cbuf);
return bbuf.array();
}
回答by Archimedes Trajano
Java SE 6 or Java EE 5 provides a method to do this now so there is no need for extra libraries.
Java SE 6 或 Java EE 5 现在提供了一种方法来执行此操作,因此不需要额外的库。
The method is DatatypeConverter.parseHexBinary
方法是DatatypeConverter.parseHexBinary
In this case it can be used as follows:
在这种情况下,它可以按如下方式使用:
String str = "9B7D2C34A366BF890C730641E6CECF6F";
byte[] bytes = DatatypeConverter.parseHexBinary(str);
The class also provides type conversions for many other formats that are generally used in XML.
该类还为通常在 XML 中使用的许多其他格式提供类型转换。
回答by G M Ramesh
try this:
尝试这个:
String str = "9B7D2C34A366BF890C730641E6CECF6F";
String[] temp = str.split(",");
bytesArray = new byte[temp.length];
int index = 0;
for (String item: temp) {
bytesArray[index] = Byte.parseByte(item);
index++;
}
回答by Richard Smith
I assume what you need is to convert a hex string into a byte array that equals that means the same thing as that hex string? Adding this method should do it for you, without any extra library importing:
我假设您需要的是将十六进制字符串转换为字节数组,该数组等于与十六进制字符串相同的含义?添加此方法应该为您完成,无需任何额外的库导入:
public static byte[] hexToByteArray(String s) {
String[] strBytes = s.split("(?<=\G.{2})");
byte[] bytes = new byte[strBytes.length];
for(int i = 0; i < strBytes.length; i++)
bytes[i] = (byte)Integer.parseInt(strBytes[i], 16);
return bytes;
}
回答by Vjeran ?ivkovi?
I know it's late but hope it will help someone else...
我知道已经晚了,但希望它会帮助别人......
This is my code: It takes two by two hex representations contained in String and add those into byte array. It works perfectly for me.
这是我的代码:它需要包含在 String 中的两个乘两个十六进制表示并将它们添加到字节数组中。它非常适合我。
public byte[] stringToByteArray (String s) {
byte[] byteArray = new byte[s.length()/2];
String[] strBytes = new String[s.length()/2];
int k = 0;
for (int i = 0; i < s.length(); i=i+2) {
int j = i+2;
strBytes[k] = s.substring(i,j);
byteArray[k] = (byte)Integer.parseInt(strBytes[k], 16);
k++;
}
return byteArray;
}