X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/7443225d96fd830c8abb2afd49381f6b38ce5aec..c788c5a40e6ac57d613c735dd9790de52be47d5f:/src/main/java/org/asamk/signal/util/Hex.java diff --git a/src/main/java/org/asamk/signal/util/Hex.java b/src/main/java/org/asamk/signal/util/Hex.java index 9f885791..f7a870ca 100644 --- a/src/main/java/org/asamk/signal/util/Hex.java +++ b/src/main/java/org/asamk/signal/util/Hex.java @@ -9,10 +9,24 @@ public class Hex { private Hex() { } + public static String toString(byte[] bytes) { + if (bytes.length == 0) { + return ""; + } + + var buf = new StringBuffer(); + for (final var aByte : bytes) { + appendHexChar(buf, aByte); + buf.append(" "); + } + buf.deleteCharAt(buf.length() - 1); + return buf.toString(); + } + public static String toStringCondensed(byte[] bytes) { - StringBuffer buf = new StringBuffer(); - for (int i = 0; i < bytes.length; i++) { - appendHexChar(buf, bytes[i]); + var buf = new StringBuffer(); + for (final var aByte : bytes) { + appendHexChar(buf, aByte); } return buf.toString(); } @@ -20,13 +34,12 @@ public class Hex { private static void appendHexChar(StringBuffer buf, int b) { buf.append(HEX_DIGITS[(b >> 4) & 0xf]); buf.append(HEX_DIGITS[b & 0xf]); - buf.append(" "); } public static byte[] toByteArray(String s) { - int len = s.length(); - byte[] data = new byte[len / 2]; - for (int i = 0; i < len; i += 2) { + var len = s.length(); + var data = new byte[len / 2]; + for (var 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;