X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/0744dcccf117064dcf6c8dbb1312565c0f3e15cb..d91e20e1f8a8626ce8e5d5f11535dc05f3c63f31:/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 new file mode 100644 index 00000000..696ca62b --- /dev/null +++ b/src/main/java/org/asamk/signal/util/Hex.java @@ -0,0 +1,31 @@ +package org.asamk.signal; + +public class Hex { + + private final static char[] HEX_DIGITS = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' + }; + + public static String toStringCondensed(byte[] bytes) { + StringBuffer buf = new StringBuffer(); + for (int i = 0; i < bytes.length; i++) { + appendHexChar(buf, bytes[i]); + } + return buf.toString(); + } + + 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) { + data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); + } + return data; + } +}