X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/2ab70edc68eddbd8e20a0956ad0fec0982e45323..43face8ead95344e3d0dcbc94c6cf85959f26203:/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 95b2d26f..f7a870ca 100644 --- a/src/main/java/org/asamk/signal/util/Hex.java +++ b/src/main/java/org/asamk/signal/util/Hex.java @@ -9,9 +9,23 @@ 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 (final byte aByte : bytes) { + 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;