Skip to content

Instantly share code, notes, and snippets.

@erlangparasu
Created June 19, 2018 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save erlangparasu/b2f650ceec0ae4d8a3eaf1b19a02e099 to your computer and use it in GitHub Desktop.
Save erlangparasu/b2f650ceec0ae4d8a3eaf1b19a02e099 to your computer and use it in GitHub Desktop.
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}
// Ref: https://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java/140861#140861
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment