Java 将十六进制字符串转换为字节抛出NumberFormatException中的二进制字符串
这是我收到的错误信息Java 将十六进制字符串转换为字节抛出NumberFormatException中的二进制字符串,java,binary,hex,numberformatexception,Java,Binary,Hex,Numberformatexception,这是我收到的错误信息 public static byte[][] keyArray = new byte[4][4]; String hex = "93"; String hexInBinary = Integer.toBinaryString(Integer.parseInt(hex, 16)); keyArray[row][col] = Byte.parseByte(hexInBinary,2); //this line causes the error 我不想使用getBytes()
public static byte[][] keyArray = new byte[4][4];
String hex = "93";
String hexInBinary = Integer.toBinaryString(Integer.parseInt(hex, 16));
keyArray[row][col] = Byte.parseByte(hexInBinary,2); //this line causes the error
我不想使用getBytes(),因为我实际上有一个长字符串“0A935D114965532BC1004865ABDCA42950”。我想一次读取2个十六进制并转换为字节
编辑:
我是如何修复的:
"Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"10010011" Radix:2."
在异常消息中写入时,您试图转换为字节的字符串超过了字节的最大值 在您的示例中,字符串“10010011”等于147,但字节变量的最大值为2^7-1=127 您可能需要检查字节类文档; 因此我建议使用
Integer.parseInt()
,而不是parseByte方法,然后将int值转换为byte,当您将整数值147转换为byte值时,它将变成-109
String hexInBinary = String.format("%8s", Integer.toBinaryString(Integer.parseInt(hex, 16))).replace(' ', '0');
keyArray[row][col] = (byte)Integer.parseInt(hexInBinary, 2);
可以看出,parseByte检测到一个值“大于”一个
字节,根据
因此,二进制表示并不是二者的互补。如果高位为1,则字节10011的两个互补表示法将为负数。因此,如果您想获得与两个互补字节10011相同的值,则需要执行byte.parseByte(“-1101100”)代码>
换句话说,一个无符号字节的最大值是255。有符号字节可以具有的最大值为127(因为如果最高有效位为1,则将其解释为负数)。但是,parseByte()
的问题在于它使用了一个明确的“-”符号而不是1来表示负数。这意味着“字节”的其余部分只能使用7位。一个字节是有符号值-128到127。@arshajii:byte.parseByte(“10010011”,2)
对我来说失败,他发布了一个异常(Java 7)。@vanza这个问题后来被更新为确实产生异常的问题。我将取消我的投票。谢谢大家!我修好了!!!!Integer.parseInt()修复了这个问题!!这并不能真正回答问题。“10010011”只有8位,是字节的有效二进制表示形式(-109在2的补码中,准确地说,这是JLS指定的)。奇怪的是API拒绝解析它。为什么它不回答这个问题?我正在解释错误的原因,并向他建议一个可行的解决方案。我同意API拒绝它是很奇怪的,但我们现在对此无能为力,如果它不起作用,我们必须找到另一个解决方案,因为“10011”是一个有效字节。任何8位都是有效字节。你只是提供了一个变通方法。这个答案的第一行是不正确的。一个字节的最大值为255。有符号字节可以具有的最大值为127(因为如果最高有效位为1,则将其解释为负数)。然而,parseByte
的问题在于它使用了一个明确的“-”符号而不是1来表示负数。这意味着“字节”的其余部分只能使用7位。请参阅我在问题本身下方的评论中发布的链接。似乎该行为故意与语言规范不一致:-/为了好玩:Integer.parseInt(Integer.tobinarysting(-1),2)
。可能与文档一致,但未通过。
public class ByteConvert {
public static void main(String[] argv) {
String hex = "93";
String hexInBinary = Integer.toBinaryString(Integer.parseInt(hex, 16));
int intResult = Integer.parseInt(hexInBinary,2);
System.out.println("intResult = " + intResult);
byte byteResult = (byte) (Integer.parseInt(hexInBinary,2));
System.out.println("byteResult = " + byteResult);
byte result = Byte.parseByte(hexInBinary,2);
System.out.println("result = " + result);
}
}
C:\JavaTools>java ByteConvert
intResult = 147
byteResult = -109
Exception in thread "main" java.lang.NumberFormatException: Value out of range.
Value:"10010011" Radix:2
at java.lang.Byte.parseByte(Unknown Source)
at ByteConvert.main(ByteConvert.java:9)
"The characters in the string must all be digits, of the specified radix
(as determined by whether Character.digit(char, int) returns a nonnegative
value) except that the first character may be an ASCII minus
sign '-' ('\u002D') to indicate a negative value."