gpt4 book ai didi

java.lang.NumberFormatException : Value out of range. 值 :"8C"Radix:16 从 String 转换为 byte[]

转载 作者:行者123 更新时间:2023-12-02 01:49:16 24 4
gpt4 key购买 nike

<分区>

从以下代码中获取以下错误。我想要实现的是将一系列十六进制代码作为字节本身打印到文件中。我该如何修复它,以便我可以在文件中打印 8C

public static void process() {
System.out.println("File to print");
String hexString = "418C";
try {
byte value[] = getByte(hexString);
try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
outputStream.write(value);
}
} catch (Exception exp) {
exp.printStackTrace();
}
}

private static byte[] getByte(String str) {
byte[] val = new byte[str.length() / 2];
for (int i = 0; i < val.length; i++) {
int index = i * 2;
byte byt = Byte.parseByte(str.substring(index, index + 2), 16);
val[i] = byt;
}
return val;
}

异常

java.lang.NumberFormatException: Value out of range. Value:"8C" Radix:16
at java.base/java.lang.Byte.parseByte(Byte.java:154)

根据以下链接,我更改为 Character.MAX_RADIX,但出现另一个错误。 https://www.tutorialspoint.com/java/lang/byte_parsebyte_radix.htm

以下链接很有帮助 Why does Byte.parseByte("80", 16) fail?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com