Skip to content

Instantly share code, notes, and snippets.

@madsunrise
Created July 13, 2017 11:37
Show Gist options
  • Select an option

  • Save madsunrise/c676c12f70d8d8e6a05af676fb337c9f to your computer and use it in GitHub Desktop.

Select an option

Save madsunrise/c676c12f70d8d8e6a05af676fb337c9f to your computer and use it in GitHub Desktop.
JAVA
private String getHexCounter(int i) {
String hex = Integer.toHexString(i);
switch (hex.length()) {
case 1:
return '0' + hex + " 00";
case 2:
return hex + " 00";
case 3:
return hex.substring(1) + " 0" + hex.charAt(0);
case 4:
return hex.substring(2) + ' ' + hex.substring(0, 2);
default:
return "ololo";
}
}
private String toHexString(byte[] array, int limit) {
char[] HEX_CHARS = "0123456789ABCDEF".toCharArray();
StringBuffer result = new StringBuffer();
if (array.length == 0) {
return "";
}
for (int i = 0; i < array.length; ++i) {
if (i == limit) {
break;
}
int octet = array[i];
int firstIndex = (octet & 0xF0) >>> 4;
int secondIndex = octet & 0x0F;
result.append(HEX_CHARS[firstIndex]);
result.append(HEX_CHARS[secondIndex]);
result.append(' ');
}
String res = result.toString();
return res.substring(0, res.length() - 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment