如何在Java中将字节数组转换为十六进制格式

Jam*_*ade 12 java format stringbuilder hex bytearray

我知道您可以使用printf并使用StringBuilder.append(String.format("%x", byte))将值转换为HEX值并在控制台上显示它们.但我希望能够实际格式化字节数组,以便每个字节显示为HEX而不是十进制.

这是我的代码的一部分,我已经说过我说的前两种方式:

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;
Run Code Online (Sandbox Code Playgroud)

分成字节数组的字符串是:

"1E2021345A2B"
Run Code Online (Sandbox Code Playgroud)

但在控制台上将其显示为十进制:

"303233529043"
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我如何使实际值以十六进制显示并自然地以这种方式显示.先感谢您.

Pau*_*gas 30

你可以使用String javax.xml.bind.DatatypeConverter.printHexBinary(byte[]).例如:

public static void main(String[] args) {
    byte[] array = new byte[] { 127, 15, 0 };
    String hex = DatatypeConverter.printHexBinary(array);
    System.out.println(hex); // prints "7F0F00"
}
Run Code Online (Sandbox Code Playgroud)

  • 我发现的任何其他方式的最短路径. (2认同)

VGR*_*VGR 23

String.format实际上使用了java.util.Formatter类.而不是使用String.format方便方法,直接使用Formatter:

Formatter formatter = new Formatter();
for (byte b : bytes) {
    formatter.format("%02x", b);
}
String hex = formatter.toString();
Run Code Online (Sandbox Code Playgroud)


Tam*_*mas 5

我的方式:

  private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
      'B', 'C', 'D', 'E', 'F' };

  public static String toHexString(byte[] bytes) {
    char[] hexChars = new char[bytes.length * 2];
    int v;
    for (int j = 0; j < bytes.length; j++) {
      v = bytes[j] & 0xFF;
      hexChars[j * 2] = HEX_CHARS[v >>> 4];
      hexChars[j * 2 + 1] = HEX_CHARS[v & 0x0F];
    }
    return new String(hexChars);
  }
Run Code Online (Sandbox Code Playgroud)