【发布时间】:2013-11-09 09:32:17
【问题描述】:
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
这是我收到的错误消息,
"Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"10010011" Radix:2."
我不想使用 getBytes(),因为我实际上有一个长字符串,“0A935D11496532BC1004865ABDCA42950”。我想一次读取 2 个十六进制并转换为字节。
编辑:
我是如何解决的:
String hexInBinary = String.format("%8s", Integer.toBinaryString(Integer.parseInt(hex, 16))).replace(' ', '0');
keyArray[row][col] = (byte)Integer.parseInt(hexInBinary, 2);
【问题讨论】:
-
一个字节是有符号的值 -128 到 127。
-
@arshajii:
Byte.parseByte("10010011", 2)对我来说失败了,除了他发布的例外(Java 7)。 -
@vanza 该问题已更新为确实会产生异常的问题。我将删除我的近距离投票。
-
谢谢大家!我修好了它!!!! Integer.parseInt() 解决了问题!!
标签: java binary hex numberformatexception