【发布时间】:2017-08-23 17:26:45
【问题描述】:
我得到了一个像“0xFF”这样的十六进制字符串,并且想将该字符串转换为字节 0xFF,因为其他函数需要该值作为十六进制字节。所以是这样的:
String hexstring="0xFF";
//convert to byte
byte hexbyte = (byte) 0xFF;
感谢您的帮助
【问题讨论】:
我得到了一个像“0xFF”这样的十六进制字符串,并且想将该字符串转换为字节 0xFF,因为其他函数需要该值作为十六进制字节。所以是这样的:
String hexstring="0xFF";
//convert to byte
byte hexbyte = (byte) 0xFF;
感谢您的帮助
【问题讨论】:
public static byte[] asByteArray(String hex) {
// Create a byte array half the length of the string.
byte[] bytes = new byte[hex.length() / 2];
// Repeat the process for the number of elements in the byte array.
for (int index = 0; index < bytes.length; index++) {
// Convert hexadecimal string to bytes and store in array.
bytes[index] =
(byte) Integer.parseInt(
hex.substring(index * 2, (index + 1) * 2),
16);
}
// return byte array。
return bytes;
}
【讨论】:
(byte) (Integer.parseInt("ef",16) & 0xff);将为您工作
【讨论】: