问题描述
我知道你可以使用 这是我已经有一段代码(b> 分成字节数组的字符串是: 但显示它作为控制台上的十进制数: 任何人都可以请帮助我如何获得实际值在十六进制并自然显示。提前感谢您。 您可以使用。例如: I know that you can use Here is a section of my code that I have already that does the first two ways that I stated: The string that was split up into the byte array was: But displays it as decimal on the console as: Could anyone please help me on how to get the actual values to be in hex and be displayed in that way naturally. Thank you in advance. You can use the 这篇关于如何在Java中将字节数组转换为十六进制格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! printf
并且使用 StringBuilder.append(String.format(%x,byte) code>将值转换为HEX值并在控制台上显示它们。但我希望能够实际上格式化字节数组,使每个字节显示为HEX而不是十进制。
if(bytes> 0)
{
byteArray =新的字节[字节]; //设置数组来接收这些值。
(int i = 0; i {
byteString = hexSubString(hexString,offSet,CHARSPERBYTE,false); //为一个字节隔离数字。
Log.d(HEXSTRING,byteString); (byteString.length()> 0)
byteArray [i] =(byte)Integer.parseInt(byteString,16); //将值解析为二进制数据数组。
}
else
{
System.out.println(String is empty!);
}
offSet + = CHARSPERBYTE; //设置下一个字十六进制。
}
StringBuilder sb = new StringBuilder();
for(byte b:byteArray)
{
sb.append(String.format(%x,b));
}
字节subSystem = byteArray [0];
字节highLevel = byteArray [1];
字节lowLevel = byteArray [2];
System.out.println(Byte array size:+ byteArray.length);
System.out.printf(Byte 1:+%x,subSystem);
System.out.printf(Byte 2:+%x,highLevel);
System.out.println(Byte 3:+ lowLevel);
System.out.println(Byte array value:+ Arrays.toString(byteArray));
System.out.println(Byte array values as HEX:+ sb.toString());
}
else
{
byteArray = new byte [0]; //没有十六进制数据。
// throw new HexException();
}
return byteArray;
1E2021345A2B
303233529043
public static void main(String [] args){
byte [] array = new byte [] {127,15,0};
String hex = DatatypeConverter.printHexBinary(array);
System.out.println(hex); //打印7F0F00
}
printf
and also use StringBuilder.append(String.format("%x", byte))
to convert values to HEX values and display them on the console. But I want to be able to actually format the byte array so that each byte is displayed as HEX instead of decimal.if(bytes > 0)
{
byteArray = new byte[bytes]; // Set up array to receive these values.
for(int i=0; i<bytes; i++)
{
byteString = hexSubString(hexString, offSet, CHARSPERBYTE, false); // Isolate digits for a single byte.
Log.d("HEXSTRING", byteString);
if(byteString.length() > 0)
{
byteArray[i] = (byte)Integer.parseInt(byteString, 16); // Parse value into binary data array.
}
else
{
System.out.println("String is empty!");
}
offSet += CHARSPERBYTE; // Set up for next word hex.
}
StringBuilder sb = new StringBuilder();
for(byte b : byteArray)
{
sb.append(String.format("%x", b));
}
byte subSystem = byteArray[0];
byte highLevel = byteArray[1];
byte lowLevel = byteArray[2];
System.out.println("Byte array size: " + byteArray.length);
System.out.printf("Byte 1: " + "%x", subSystem);
System.out.printf("Byte 2: " + "%x", highLevel);
System.out.println("Byte 3: " + lowLevel);
System.out.println("Byte array value: " + Arrays.toString(byteArray));
System.out.println("Byte array values as HEX: " + sb.toString());
}
else
{
byteArray = new byte[0]; // No hex data.
//throw new HexException();
}
return byteArray;
"1E2021345A2B"
"303233529043"
String javax.xml.bind.DatatypeConverter.printHexBinary(byte[])
. e.g.:public static void main(String[] args) {
byte[] array = new byte[] { 127, 15, 0 };
String hex = DatatypeConverter.printHexBinary(array);
System.out.println(hex); // prints "7F0F00"
}