From 012235d05d8dc7c2decdc7229d93033b0399ecbb Mon Sep 17 00:00:00 2001
From: xiejun <xiejun@vci-tech.com>
Date: 星期日, 10 十一月 2024 15:49:53 +0800
Subject: [PATCH] 集成获取mdm分发通用数据格式接口集成
---
Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/HexUtil.java | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 164 insertions(+), 0 deletions(-)
diff --git a/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/HexUtil.java b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/HexUtil.java
new file mode 100644
index 0000000..da3f452
--- /dev/null
+++ b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/HexUtil.java
@@ -0,0 +1,164 @@
+/*
+ * Copyright (c) 2018-2028, DreamLu All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * Neither the name of the dreamlu.net developer nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * Author: DreamLu 鍗㈡槬姊� (596392912@qq.com)
+ */
+package org.springblade.core.tool.utils;
+
+import org.springframework.lang.Nullable;
+
+import java.nio.charset.Charset;
+
+/**
+ * hex 宸ュ叿锛岀紪瑙g爜鍏ㄧ敤 byte
+ *
+ * @author L.cm
+ */
+public class HexUtil {
+ public static final Charset DEFAULT_CHARSET = Charsets.UTF_8;
+ private static final byte[] DIGITS_LOWER = new byte[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
+ private static final byte[] DIGITS_UPPER = new byte[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
+
+ /**
+ * encode Hex
+ *
+ * @param data data to hex
+ * @return hex bytes
+ */
+ public static byte[] encode(byte[] data) {
+ return encode(data, true);
+ }
+
+ /**
+ * encode Hex
+ *
+ * @param data data to hex
+ * @param toLowerCase 鏄惁灏忓啓
+ * @return hex bytes
+ */
+ public static byte[] encode(byte[] data, boolean toLowerCase) {
+ return encode(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
+ }
+
+ /**
+ * encode Hex
+ *
+ * @param data Data to Hex
+ * @return bytes as a hex string
+ */
+ private static byte[] encode(byte[] data, byte[] digits) {
+ int len = data.length;
+ byte[] out = new byte[len << 1];
+ for (int i = 0, j = 0; i < len; i++) {
+ out[j++] = digits[(0xF0 & data[i]) >>> 4];
+ out[j++] = digits[0xF & data[i]];
+ }
+ return out;
+ }
+
+ /**
+ * encode Hex
+ *
+ * @param data Data to Hex
+ * @param toLowerCase 鏄惁灏忓啓
+ * @return bytes as a hex string
+ */
+ public static String encodeToString(byte[] data, boolean toLowerCase) {
+ return new String(encode(data, toLowerCase), DEFAULT_CHARSET);
+ }
+
+ /**
+ * encode Hex
+ *
+ * @param data Data to Hex
+ * @return bytes as a hex string
+ */
+ public static String encodeToString(byte[] data) {
+ return new String(encode(data), DEFAULT_CHARSET);
+ }
+
+ /**
+ * encode Hex
+ *
+ * @param data Data to Hex
+ * @return bytes as a hex string
+ */
+ @Nullable
+ public static String encodeToString(@Nullable String data) {
+ if (StringUtil.isBlank(data)) {
+ return null;
+ }
+ return encodeToString(data.getBytes(DEFAULT_CHARSET));
+ }
+
+ /**
+ * decode Hex
+ *
+ * @param data Hex data
+ * @return decode hex to bytes
+ */
+ @Nullable
+ public static byte[] decode(@Nullable String data) {
+ if (StringUtil.isBlank(data)) {
+ return null;
+ }
+ return decode(data.getBytes(DEFAULT_CHARSET));
+ }
+
+ /**
+ * encode Hex
+ *
+ * @param data Data to Hex
+ * @return bytes as a hex string
+ */
+ @Nullable
+ public static String decodeToString(@Nullable String data) {
+ byte[] decodeBytes = decode(data);
+ if (decodeBytes == null) {
+ return null;
+ }
+ return new String(decodeBytes, DEFAULT_CHARSET);
+ }
+
+ /**
+ * decode Hex
+ *
+ * @param data Hex data
+ * @return decode hex to bytes
+ */
+ public static byte[] decode(byte[] data) {
+ int len = data.length;
+ if ((len & 0x01) != 0) {
+ throw new IllegalArgumentException("hexBinary needs to be even-length: " + len);
+ }
+ byte[] out = new byte[len >> 1];
+ for (int i = 0, j = 0; j < len; i++) {
+ int f = toDigit(data[j], j) << 4;
+ j++;
+ f |= toDigit(data[j], j);
+ j++;
+ out[i] = (byte) (f & 0xFF);
+ }
+ 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 " + b + " at index " + index);
+ }
+ return digit;
+ }
+
+}
--
Gitblit v1.9.3