diff options
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/util/BytesUtil.java')
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/util/BytesUtil.java | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/util/BytesUtil.java b/src/main/java/com/keuin/kbackupfabric/util/BytesUtil.java new file mode 100644 index 0000000..c33c028 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/BytesUtil.java @@ -0,0 +1,31 @@ +package com.keuin.kbackupfabric.util; + +import java.nio.charset.StandardCharsets; + +public class BytesUtil { + private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII); + + public static String bytesToHex(byte[] bytes) { + byte[] hexChars = new byte[bytes.length * 2]; + for (int j = 0; j < bytes.length; j++) { + int v = bytes[j] & 0xFF; + hexChars[j * 2] = HEX_ARRAY[v >>> 4]; + hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; + } + return new String(hexChars, StandardCharsets.UTF_8).toUpperCase(); + } + + public static byte[] hexToBytes(String s) { + int len = s.length(); + if (len % 2 != 0) + throw new IllegalArgumentException("Invalid hex string."); + byte[] b = new byte[len / 2]; + int index, v; + for (int i = 0; i < b.length; i++) { + index = i * 2; + v = Integer.parseInt(s.substring(index, index + 2), 16); + b[i] = (byte) v; + } + return b; + } +} |