Skip to content

Instantly share code, notes, and snippets.

@hpcslag
Created November 8, 2017 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hpcslag/bd0c4b0e9e897c5f0f058ef151d0785e to your computer and use it in GitHub Desktop.
Save hpcslag/bd0c4b0e9e897c5f0f058ef151d0785e to your computer and use it in GitHub Desktop.
Put byte arrays to hex string decoder.
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
//examples:
// (byte)-112, (byte)0, (byte)1 -> 0x90, 0x00, 0x01
System.out.println(bytesToHex(new byte[]{-112,0,1}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment