我有以下代码。
String test1 = "10";
String result = String.format("%02X", test1);
char buffer[] = {result.charAt(0),0x01,0x00,0x01,0x00,0x20};
byte[] bufferbyte = new String(buffer).getBytes();
for (byte b : bufferbyte){
System.out.format("0X%x ", b);
}
实际上字符串变量“test1”包含十进制值。我的意思是说用户可以输入 1 -256 个整数,并将其存储在“test1”中。我以 10 为例。我需要将其十六进制值(0A)附加到字符数组中,在其第一个位置“缓冲区”并将其显示为包含十六进制值的字节数组。
上面的代码显示错误为
"Exception in thread "main" java.util.IllegalFormatConversionException: x != java.lang.String"
最佳答案
试试这个
String result = String.format("%02X", Integer.parseInt(test1));
关于java - 将字符串格式化为十六进制值并将其附加到字符数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20964127/