Skip to content

Instantly share code, notes, and snippets.

@darkstone
Forked from FoxIvan/toHex.md
Created September 27, 2018 04:48
Show Gist options
  • Save darkstone/9ad68b3c96514755d1d01cbcc0cc9fa3 to your computer and use it in GitHub Desktop.
Save darkstone/9ad68b3c96514755d1d01cbcc0cc9fa3 to your computer and use it in GitHub Desktop.
bytes to hex
  private final static char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
  
  public static String toHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int i = 0; i < bytes.length; i++) {
            int byteValue = bytes[i] & 0xFF;
            hexChars[i * 2] = HEX_ARRAY[byteValue >>> 4];
            hexChars[i * 2 + 1] = HEX_ARRAY[byteValue & 0x0F];
        }
        return new String(hexChars);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment