package com.qianwen.core.tool.utils; import java.nio.charset.Charset; import org.springframework.lang.Nullable; /* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/utils/HexUtil.class */ public class HexUtil { public static final Charset DEFAULT_CHARSET = Charsets.UTF_8; private static final byte[] DIGITS_LOWER = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102}; private static final byte[] DIGITS_UPPER = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70}; public static byte[] encode(byte[] data) { return encode(data, true); } public static byte[] encode(byte[] data, boolean toLowerCase) { return encode(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER); } private static byte[] encode(byte[] data, byte[] digits) { int len = data.length; byte[] out = new byte[len << 1]; int j = 0; for (int i = 0; i < len; i++) { int i2 = j; int j2 = j + 1; out[i2] = digits[(240 & data[i]) >>> 4]; j = j2 + 1; out[j2] = digits[15 & data[i]]; } return out; } public static String encodeToString(byte[] data, boolean toLowerCase) { return new String(encode(data, toLowerCase), DEFAULT_CHARSET); } public static String encodeToString(byte[] data) { return new String(encode(data), DEFAULT_CHARSET); } @Nullable public static String encodeToString(@Nullable String data) { if (StringUtil.isBlank(data)) { return null; } return encodeToString(data.getBytes(DEFAULT_CHARSET)); } @Nullable public static byte[] decode(@Nullable String data) { if (StringUtil.isBlank(data)) { return null; } return decode(data.getBytes(DEFAULT_CHARSET)); } @Nullable public static String decodeToString(@Nullable String data) { byte[] decodeBytes = decode(data); if (decodeBytes == null) { return null; } return new String(decodeBytes, DEFAULT_CHARSET); } public static byte[] decode(byte[] data) { int len = data.length; if ((len & 1) != 0) { throw new IllegalArgumentException("hexBinary needs to be even-length: " + len); } byte[] out = new byte[len >> 1]; int i = 0; int j = 0; while (j < len) { int f = toDigit(data[j], j) << 4; int j2 = j + 1; int f2 = f | toDigit(data[j2], j2); j = j2 + 1; out[i] = (byte) (f2 & 255); i++; } return out; } private static int toDigit(byte b, int index) { int digit = Character.digit(b, 16); if (digit == -1) { throw new IllegalArgumentException("Illegal hexadecimal byte " + ((int) b) + " at index " + index); } return digit; } }