From 14de918a79943e4961b09fa01ed320c6cad41f2e Mon Sep 17 00:00:00 2001 From: wjc <1243177876@qq.com> Date: 星期三, 28 六月 2023 17:14:51 +0800 Subject: [PATCH] Revert "Revert "Merge branch 'hxb' into wjc"" --- HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/HexUtil.java | 449 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 449 insertions(+), 0 deletions(-) diff --git a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/HexUtil.java b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/HexUtil.java new file mode 100644 index 0000000..41373e0 --- /dev/null +++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/HexUtil.java @@ -0,0 +1,449 @@ +package com.hdl.sdk.link.core.utils; + +import android.util.Log; + +import java.math.BigDecimal; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.Date; + +/** + * Created by Zoro + * Created on 2021/9/17 + * description: + */ +public class HexUtil { + private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5', + '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + + private static final char[] DIGITS_UPPER = {'0', '1', '2', '3', '4', '5', + '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; + + public static char[] encodeHex(byte[] data) { + return encodeHex(data, true); + } + + public static char[] encodeHex(byte[] data, boolean toLowerCase) { + return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER); + } + + protected static char[] encodeHex(byte[] data, char[] toDigits) { + if (data == null) + return null; + int l = data.length; + char[] out = new char[l << 1]; + for (int i = 0, j = 0; i < l; i++) { + out[j++] = toDigits[(0xF0 & data[i]) >>> 4]; + out[j++] = toDigits[0x0F & data[i]]; + } + return out; + } + + + public static String encodeHexStr(byte[] data) { + return encodeHexStr(data, true); + } + + public static String encodeHexStr(byte[] data, boolean toLowerCase) { + return encodeHexStr(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER); + } + + + protected static String encodeHexStr(byte[] data, char[] toDigits) { + return new String(encodeHex(data, toDigits)); + } + + public static String formatHexString(byte[] data) { + return formatHexString(data, false); + } + + public static String formatHexString(byte[] data, boolean addSpace) { + if (data == null || data.length < 1) + return null; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < data.length; i++) { + String hex = Integer.toHexString(data[i] & 0xFF); + if (hex.length() == 1) { + hex = '0' + hex; + } + sb.append(hex); + if (addSpace) + sb.append(" "); + } + return sb.toString().trim(); + } + + public static byte[] decodeHex(char[] data) { + + int len = data.length; + + if ((len & 0x01) != 0) { + throw new RuntimeException("Odd number of characters."); + } + + byte[] out = new byte[len >> 1]; + + // two characters form the hex value. + for (int i = 0, j = 0; j < len; i++) { + int f = toDigit(data[j], j) << 4; + j++; + f = f | toDigit(data[j], j); + j++; + out[i] = (byte) (f & 0xFF); + } + + return out; + } + + /** + * 灏嗗崄鍏繘鍒跺瓧绗︿覆瑙g爜涓篵yte[] + * + * @param hexStr 鍗佸叚杩涘埗String + * @return byte[] + */ + public static byte[] decodeHex(String hexStr) { + if ("".equals(hexStr)) { + return null; + } + hexStr = hexStr.replace(" ",""); + return decodeHex(hexStr.toCharArray()); + } + + public static byte[] addAll(byte[]... arrays) { + if (arrays.length == 1) { + return arrays[0]; + } + + // 璁$畻鎬婚暱搴� + int length = 0; + for (byte[] array : arrays) { + if (null != array) { + length += array.length; + } + } + + final byte[] result = new byte[length]; + length = 0; + for (byte[] array : arrays) { + if (null != array) { + System.arraycopy(array, 0, result, length, array.length); + length += array.length; + } + } + return result; + } + + + + protected static int toDigit(char ch, int index) { + int digit = Character.digit(ch, 16); + if (digit == -1) { + throw new RuntimeException("Illegal hexadecimal character " + ch + + " at index " + index); + } + return digit; + } + + + public static byte[] hexStringToBytes(String hexString) { + if (hexString == null || hexString.equals("")) { + return null; + } + hexString = hexString.toUpperCase(); + int length = hexString.length() / 2; + char[] hexChars = hexString.toCharArray(); + byte[] d = new byte[length]; + for (int i = 0; i < length; i++) { + int pos = i * 2; + d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); + } + return d; + } + + public static byte charToByte(char c) { + return (byte) "0123456789ABCDEF".indexOf(c); + } + + public static String extractData(byte[] data, int position) { + return HexUtil.formatHexString(new byte[]{data[position]}); + } + + public static String bytesToHexString(byte[] data) { + String result = ""; + for (int i = 0; i < data.length; i++) { + String hexString = Integer.toHexString(data[i] & 0xFF); + if (hexString.length() == 1) { + hexString = '0' + hexString; + } + result += hexString.toUpperCase(); + } + return result; + } + + /** + * byte杞�16杩涘埗 + * + * @param b + * @return + */ + public static String binaryToHex(byte b) { + String str = byteToBit(b); + String hexStr = Integer.toHexString(Integer.parseInt(str, 2)); + StringBuffer stringBuffer = new StringBuffer(); + if (hexStr.length() == 1) { + stringBuffer.append("0"); + } + stringBuffer.append(hexStr); + return stringBuffer.toString().toUpperCase(); + } + + /** + * byte杞崄杩涘埗 + * + * @param b + * @return + */ + public static int binaryToDecimal(byte b) { + String str = byteToBit(b); + return Integer.parseInt(str, 2); + } + + /** + * Byte杞珺it + */ + public static String byteToBit(byte b) { + return "" + (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1) + + (byte) ((b >> 5) & 0x1) + (byte) ((b >> 4) & 0x1) + + (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1) + + (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1); + } + + /** + * int杞琤yte + * + * @param res + * @return + */ + public static byte[] int2byte(int res) { + byte[] targets = new byte[2]; + + targets[1] = (byte) (res & 0xff);// 鏈�浣庝綅 + targets[0] = (byte) ((res >> 8) & 0xff);// 娆′綆浣� + /*targets[2] = (byte) ((res >> 16) & 0xff);// 娆¢珮浣� + targets[3] = (byte) (res >>> 24);// 鏈�楂樹綅,鏃犵鍙峰彸绉�*/ + return targets; + } + + /** + * Bit杞珺yte + */ + public static byte bitToByte(String byteStr) { + int re, len; + if (null == byteStr) { + return 0; + } + len = byteStr.length(); + if (len != 4 && len != 8) { + return 0; + } + if (len == 8) {// 8 bit澶勭悊 + if (byteStr.charAt(0) == '0') {// 姝f暟 + re = Integer.parseInt(byteStr, 2); + } else {// 璐熸暟 + re = Integer.parseInt(byteStr, 2) - 256; + } + } else {// 4 bit澶勭悊 + re = Integer.parseInt(byteStr, 2); + } + return (byte) re; + } + + /** + * 瑙f瀽浣撹剛鏁版嵁 + * + * @param first + * @param second + * @param b + * @return + */ + public static double getData(int first, int second, byte[] b) { + double data = ((b[first] & 0xFF) << 8) + (b[second] & 0xFF); + Log.e("", "data = " + data); + return data / 10; + } + + /** + * 瑙f瀽浣撹剛鏁版嵁 + * + * @param first + * @param second + * @param b + * @return + */ + public static int getDataInt(int first, int second, byte[] b) { + int data = ((b[first] & 0xFF) << 8) + (b[second] & 0xFF); + Log.e("", "getDataInt = " + data); + return data; + } + + + // 鑾峰彇绯荤粺鏃堕棿 + public static String getTime() { + SimpleDateFormat formatter = new SimpleDateFormat("HHmmss"); + Date curDate = new Date(System.currentTimeMillis()); + String str = formatter.format(curDate); + return str; + } + + // 鑾峰彇绯荤粺鏃ユ湡 + public static String getDate() { + SimpleDateFormat formatter = new SimpleDateFormat("yyMMdd"); + Date curDate = new Date(System.currentTimeMillis()); + String str = formatter.format(curDate); + return str; + } + + /** + * 鑾峰彇褰撳墠绯荤粺鏃堕棿 + * + * @return + */ + public static String getCurrentTime() { + SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); + Date curDate = new Date(System.currentTimeMillis()); + String str = formatter.format(curDate); + return str; + } + + /** + * 鍚堝苟鏁扮粍 + * + * @return + */ + public static byte[] concat(byte[] b, byte[] mByte) { + byte[] result = Arrays.copyOf(b, b.length + mByte.length); + System.arraycopy(mByte, 0, result, b.length, mByte.length); + return result; + } + + /** + * 瀛楃涓查暱搴︾瓑浜�1鐨勮瘽锛岃ˉ0 + * + * @param str + * @return + */ + public static String addZero(String str) { + //L.i(ParseData.class, "addZero"); + StringBuffer sBuffer = new StringBuffer(); + if (str.length() == 1) { + sBuffer.append("0"); + sBuffer.append(str); + return sBuffer.toString(); + } else { + return str; + } + } + + public static double getPercent(double d) { + if (d > 100) { + return 0.0; + } + return d; + } + + /** + * byte鏁扮粍杞瑂tr + * + * @param b + * @return + */ + public static String byteArr2Str(byte[] b) { + if (b.length != 0) { + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append("["); + for (int i = 0; i < b.length; i++) { + stringBuffer.append(addZero(binaryToHex(b[i]))); + if (i < b.length - 1) { + stringBuffer.append(","); + } + } + stringBuffer.append("]"); + return stringBuffer.toString(); + } + return null; + } + + /** + * 淇濈暀灏忔暟鐐瑰悗count浣� + * + * @param d + * @param count + * @return + */ + public static double keepDecimal(double d, int count) { + BigDecimal decimal = new BigDecimal(d); + return decimal.setScale(count, BigDecimal.ROUND_HALF_UP).doubleValue(); + } + + /** + * kg杞琹b + * + * @param weight + * @return + */ + public static double kg2lb(double weight) { + return keepDecimal(weight * 2.2046226, 1); + } + + /** + * kg杞琷in + * + * @param weight + * @return + */ + public static double kg2jin(double weight) { + return keepDecimal(weight * 2, 1); + } + + /** + * kg杞瑂t + * + * @param weight + * @return + */ + public static String kg2st(double weight) { + double lbData = kg2lb(weight); + StringBuffer stringBuffer = new StringBuffer(); + int lb = (int) (lbData / 14); + int st = (int) lbData % 14; + stringBuffer.append(lb); + stringBuffer.append(":"); + stringBuffer.append(addZero(String.valueOf(st))); + return stringBuffer.toString(); + } + + public static String int2HexStr(int i) { + return binaryToHex(Integer.valueOf(i).byteValue()); + } + + public static byte[] hexStringToByte(String hex) { + int len = (hex.length() / 2); + byte[] result = new byte[len]; + char[] chars = hex.toCharArray(); + for (int i = 0; i < len; i++) { + int pos = i * 2; + result[i] = (byte) (toByte(chars[pos]) << 4 | toByte(chars[pos + 1])); + } + return result; + } + + private static byte toByte(char c) { + byte b = (byte) "0123456789ABCDEF".indexOf(c); + return b; + } + + public static String formatTo1(double f) { + BigDecimal bg = new BigDecimal(f); + return bg.setScale(1, BigDecimal.ROUND_HALF_UP).toString(); + } +} \ No newline at end of file -- Gitblit v1.8.0