Skip to content

Commit

Permalink
Improve toHex function
Browse files Browse the repository at this point in the history
Co-authored-by: LostLuma <[email protected]>
  • Loading branch information
Pixaurora and LostLuma committed Aug 8, 2024
1 parent 9ef215c commit e51c4c1
Showing 1 changed file with 4 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,16 @@ public static byte[] getMd5Digest(String input) {
}

public static String bytesToHex(byte[] bytes) {
List<Integer> hexDigits = new ArrayList<>();
StringBuilder hexString = new StringBuilder(bytes.length * 2);

for (byte rawByte : bytes) {
int processedByte = Byte.toUnsignedInt(rawByte);

// Example byte: 11 01 10 00
hexDigits.add(processedByte >> 4); // Grabs the part that is 11 01
hexDigits.add(processedByte & 15); // Grabs the part that is 10 00 (15 is 2 << 4 - 1)
hexString.append(Integer.toHexString(processedByte >> 0b100)); // Grabs the part that is 11 01
hexString.append(Integer.toHexString(processedByte & 0b1111)); // Grabs the part that is 10 00
}

String output = "";

for (int i : hexDigits) {
output += Integer.toHexString(i);
}

return output;
return hexString.toString();
}
}

0 comments on commit e51c4c1

Please sign in to comment.