Optimized ByteUtilities' getHexString function to be faster

This commit is contained in:
Obique PSWG
2015-08-08 10:59:35 -05:00
parent 8f919321c8
commit b31156d958
+8 -16
View File
@@ -36,23 +36,15 @@ public class ByteUtilities {
private static final char [] HEX = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
public static String getHexString(byte [] bytes) {
StringBuffer ret = new StringBuffer(bytes.length*2+(bytes.length>0?bytes.length-1:0));
for (byte b : bytes) {
ret.append(getHexString(b)).append(' ');
char [] data = new char[bytes.length*2+(bytes.length>0?bytes.length-1:0)];
byte b;
for (int i = 0; i < bytes.length; i+=3) {
b = bytes[i];
data[i+0] = HEX[(b&0xFF) >>> 4];
data[i+1] = HEX[b & 0x0F];
data[i+2] = ' ';
}
return new String(ret);
}
public static String getHexString(Byte [] bytes) {
StringBuffer ret = new StringBuffer(bytes.length*2+(bytes.length>0?bytes.length-1:0));
for (byte b : bytes) {
ret.append(getHexString(b)).append(' ');
}
return new String(ret);
}
public static String getHexString(byte b) {
return String.valueOf(HEX[(b&0xFF) >>> 4]) + HEX[b & 0x0F];
return new String(data);
}
public static byte [] longToBytes(long l) {