Skip to content

Instantly share code, notes, and snippets.

@galderz
Created November 16, 2011 18:10
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 galderz/1370843 to your computer and use it in GitHub Desktop.
Save galderz/1370843 to your computer and use it in GitHub Desktop.
public static void main(String[] args) {
byte[] sample1 = {-36, -4, 27, 111, 3, 49, 118, -49, 55, 74, 25, 12, 57, -96, 17, -119, -30, -113, 98, -42, -52, 49, 74, 93, 44, -94, 39, -71, -54, 37, 108, -102, -113, 82, 73, -88, 103, 88, -44, -49, -58, 127, -93, 83, 32, 124, 18, 83, 35, -116, -38, 43, -26, -87, 20, -82, -29, 36, -20, 50, 97, -18, -30, 80};
byte[] sample2 = {-104, 118, -26, -26, -67, -13, -84, -71, -34, -99, -124, 111, -83, -71, 2, 97, -9, 40, 12, -108, -127, 9, -92, -112, 1, 38, 99, 21, -126, 10, -36, 114, -26, 82, -74, 26, -14, -102, -8, -104, 55, -33, 109, 33, 97, -20, 51, 5, -122, -99, -17, -39, 59, -26, 124, -57, 6, -47, -34, -35, -79, 15, -67, -12};
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < sample1.length; i++) {
String hex = Integer.toHexString(0xFF & sample1[i]);
if (hex.length() == 1) {
// could use a for loop, but we're only dealing with a single byte
hexString.append('0');
}
hexString.append(hex);
}
System.out.println("Sample1, as byte array: " + Arrays.toString(sample1));
System.out.println("Sample1, as hex string: " + hexString);
}
results in:
Sample1, as byte array: [-36, -4, 27, 111, 3, 49, 118, -49, 55, 74, 25, 12, 57, -96, 17, -119, -30, -113, 98, -42, -52, 49, 74, 93, 44, -94, 39, -71, -54, 37, 108, -102, -113, 82, 73, -88, 103, 88, -44, -49, -58, 127, -93, 83, 32, 124, 18, 83, 35, -116, -38, 43, -26, -87, 20, -82, -29, 36, -20, 50, 97, -18, -30, 80]
Sample1, as hex string: dcfc1b6f033176cf374a190c39a01189e28f62d6cc314a5d2ca227b9ca256c9a8f5249a86758d4cfc67fa353207c1253238cda2be6a914aee324ec3261eee250
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment