From 66b091963fb0f3356f27ec094c013369bf91db89 Mon Sep 17 00:00:00 2001 From: jiazx0107@163.com <jiazx0107@163.com> Date: 星期日, 24 十二月 2023 14:02:19 +0800 Subject: [PATCH] 游仙协议解析-3 --- src/main/java/com/fzzy/api/utils/BytesUtil.java | 834 ++++++++++++++++++++++++++++++----------------------------- 1 files changed, 428 insertions(+), 406 deletions(-) diff --git a/src/main/java/com/fzzy/api/utils/BytesUtil.java b/src/main/java/com/fzzy/api/utils/BytesUtil.java index af4348e..491ee75 100644 --- a/src/main/java/com/fzzy/api/utils/BytesUtil.java +++ b/src/main/java/com/fzzy/api/utils/BytesUtil.java @@ -4,447 +4,469 @@ public class BytesUtil { - /** - * bytes杈撳嚭鍗佽繘鍒� - * - * @param bytes - * @return - */ - public static Integer bytesToInt(byte[] bytes) { - String str = ""; - int i = 0; - boolean flag = true && bytes.length > 0; - while (flag) { - Byte b = bytes[i]; - i++; - Integer bi = Byte.toUnsignedInt(b); - str += toHexString(bi); - if (i >= bytes.length || str.endsWith("EEEE")) - flag = false; - } - return Integer.parseInt(str, 16); - } + /** + * bytes杈撳嚭鍗佽繘鍒� + * + * @param bytes + * @return + */ + public static Integer bytesToInt(byte[] bytes) { + String str = ""; + int i = 0; + boolean flag = true && bytes.length > 0; + while (flag) { + Byte b = bytes[i]; + i++; + Integer bi = Byte.toUnsignedInt(b); + str += toHexString(bi); + if (i >= bytes.length || str.endsWith("EEEE")) + flag = false; + } + return Integer.parseInt(str, 16); + } - /** - * bytes杈撳嚭鍗佸叚杩涘埗瀛楃涓� - * - * @param bytes - * @return - */ - public static String bytesToString(byte[] bytes) { - String str = ""; - int i = 0; - boolean flag = true && bytes.length > 0; - while (flag) { - Byte b = bytes[i]; - i++; - Integer bi = Byte.toUnsignedInt(b); - str += toHexString(bi); - if (i >= bytes.length) - flag = false; - } - return str; - } + /** + * bytes杈撳嚭鍗佸叚杩涘埗瀛楃涓� + * + * @param bytes + * @return + */ + public static String bytesToString(byte[] bytes) { + String str = ""; + int i = 0; + boolean flag = true && bytes.length > 0; + while (flag) { + Byte b = bytes[i]; + i++; + Integer bi = Byte.toUnsignedInt(b); + str += toHexString(bi); + if (i >= bytes.length) + flag = false; + } + return str; + } - /** - * 鍗佸叚杩涘埗杞瓧鑺� - * - * @param hex - * @return - */ - public static byte hexToByte(Integer hex) { - return hex.byteValue(); - } + /** + * 鍗佸叚杩涘埗杞瓧鑺� + * + * @param hex + * @return + */ + public static byte hexToByte(Integer hex) { + return hex.byteValue(); + } - /** - * short杞�2瀛楄妭 宸茶皟鏁撮珮浣庝綅 - * - * @param number - * @return - */ - public static byte[] shortToByte(short number) { - int temp = number; - byte[] b = new byte[2]; - for (int i = 0; i < b.length; i++) { - b[i] = new Integer(temp & 0xff).byteValue();// 灏嗘渶浣庝綅淇濆瓨鍦ㄦ渶浣庝綅 - temp = temp >> 8; // 鍚戝彸绉�8浣� - } - return b; - } + /** + * short杞�2瀛楄妭 宸茶皟鏁撮珮浣庝綅 + * + * @param number + * @return + */ + public static byte[] shortToByte(short number) { + int temp = number; + byte[] b = new byte[2]; + for (int i = 0; i < b.length; i++) { + b[i] = new Integer(temp & 0xff).byteValue();// 灏嗘渶浣庝綅淇濆瓨鍦ㄦ渶浣庝綅 + temp = temp >> 8; // 鍚戝彸绉�8浣� + } + return b; + } - /** - * 浜岃繘鍒跺瓙涓茶浆16杩涘埗瀛愪覆 - * - * @param bin - * @return - */ - public static String binToHex(String bin) { - String hexStr = ""; - int size = bin.length() / 8; - for (int i = 0; i < size; i++) { - String value = bin.substring(i * 8, (i + 1) * 8); - int temp = Integer.parseInt(value, 2); - String tempHex = Integer.toHexString(temp); - hexStr += tempHex; - } - return hexStr; - } + /** + * 浜岃繘鍒跺瓙涓茶浆16杩涘埗瀛愪覆 + * + * @param bin + * @return + */ + public static String binToHex(String bin) { + String hexStr = ""; + int size = bin.length() / 8; + for (int i = 0; i < size; i++) { + String value = bin.substring(i * 8, (i + 1) * 8); + int temp = Integer.parseInt(value, 2); + String tempHex = Integer.toHexString(temp); + hexStr += tempHex; + } + return hexStr; + } - /** - * 浜岃繘鍒惰浆16杩涘埗锛屼笉瓒�2浣嶈ˉ浣� - * - * @param bin - * @return - */ - public static String bin2Hex(String bin) { - String hexStr = ""; - int size = bin.length() / 8; - for (int i = 0; i < size; i++) { - String value = bin.substring(i * 8, (i + 1) * 8); - int temp = Integer.parseInt(value, 2); - String tempHex = Integer.toHexString(temp); - if (tempHex.length() < 2) { - tempHex = "0" + tempHex; - } - hexStr += tempHex; - } + /** + * 浜岃繘鍒惰浆16杩涘埗锛屼笉瓒�2浣嶈ˉ浣� + * + * @param bin + * @return + */ + public static String bin2Hex(String bin) { + String hexStr = ""; + int size = bin.length() / 8; + for (int i = 0; i < size; i++) { + String value = bin.substring(i * 8, (i + 1) * 8); + int temp = Integer.parseInt(value, 2); + String tempHex = Integer.toHexString(temp); + if (tempHex.length() < 2) { + tempHex = "0" + tempHex; + } + hexStr += tempHex; + } - return hexStr; - } + return hexStr; + } - /** - * 杞负鍗佸叚杩涘埗涓诧紝涓嶈冻2浣嶈ˉ0 - * - * @param value - * @return - */ - public static String toHexString(int value) { - String tempHex = Integer.toHexString(value); - if (tempHex.length() < 2) { - tempHex = "0" + tempHex; - } - return tempHex.toUpperCase(); - } + /** + * 杞负鍗佸叚杩涘埗涓诧紝涓嶈冻2浣嶈ˉ0 + * + * @param value + * @return + */ + public static String toHexString(int value) { + String tempHex = Integer.toHexString(value); + if (tempHex.length() < 2) { + tempHex = "0" + tempHex; + } + return tempHex.toUpperCase(); + } - public static String getTargetId(String value, boolean isTwo) { - String tempHex = Integer.toHexString(Integer.valueOf(value)); - if (tempHex.length() < 2) { - tempHex = "0" + tempHex; - } - if (isTwo) { - if (tempHex.length() < 4) { - tempHex = "0" + tempHex; - } - if (tempHex.length() < 4) { - tempHex = "0" + tempHex; - } - } - return tempHex.toUpperCase(); - } + public static String getTargetId(String value, boolean isTwo) { + String tempHex = Integer.toHexString(Integer.valueOf(value)); + if (tempHex.length() < 2) { + tempHex = "0" + tempHex; + } + if (isTwo) { + if (tempHex.length() < 4) { + tempHex = "0" + tempHex; + } + if (tempHex.length() < 4) { + tempHex = "0" + tempHex; + } + } + return tempHex.toUpperCase(); + } - /** - * 鏍规嵁鏁板�艰幏鍙栧埌闀垮害涓�2瀛楄妭鐨�16杩涘埗瀛楃 - * - * @param value - * @return - */ - public static String getHex2LenStr(int value) { - String tempHex = Integer.toHexString(value); - if (tempHex.length() < 2) { - tempHex = "0" + tempHex; - } - if (tempHex.length() < 4) { - tempHex = "0" + tempHex; - } - if (tempHex.length() < 4) { - tempHex = "0" + tempHex; - } - return tempHex.toUpperCase(); - } + /** + * 鏍规嵁鏁板�艰幏鍙栧埌闀垮害涓�2瀛楄妭鐨�16杩涘埗瀛楃 + * + * @param value + * @return + */ + public static String getHex2LenStr(int value) { + String tempHex = Integer.toHexString(value); + if (tempHex.length() < 2) { + tempHex = "0" + tempHex; + } + if (tempHex.length() < 4) { + tempHex = "0" + tempHex; + } + if (tempHex.length() < 4) { + tempHex = "0" + tempHex; + } + return tempHex.toUpperCase(); + } - public static String getOrderId(int value) { - return getHex2LenStr(value); - } + public static String getOrderId(int value) { + return getHex2LenStr(value); + } - /** - * 灏唙alue杞负same涓浉鍚屾弧8浣嶇殑浜岃繘鍒跺瓧涓� - * - * @param value - * @param same - * @return - */ - public static String toBinary8StringSame(int value, int same) { - String tempBinStr = toBinary8String(value); - String rsBinStr = ""; - for (int i = 0; i < same; i++) { - rsBinStr += tempBinStr; - } - return rsBinStr; - } + /** + * 灏唙alue杞负same涓浉鍚屾弧8浣嶇殑浜岃繘鍒跺瓧涓� + * + * @param value + * @param same + * @return + */ + public static String toBinary8StringSame(int value, int same) { + String tempBinStr = toBinary8String(value); + String rsBinStr = ""; + for (int i = 0; i < same; i++) { + rsBinStr += tempBinStr; + } + return rsBinStr; + } - /** - * 涓嶈冻width涓瓧鑺傚搴︽椂锛屽墠闈㈣ˉ0鑷硍idth*8 - * - * @param value - * @param width - * @return - */ - public static String toBinary8String(int value, int width) { - String tempBinStr = toBinary8String(value); - int size = tempBinStr.length(); - for (int i = 0; i < width; i++) { - int temp = (i + 1) * 8; - if (temp > size) { - tempBinStr = "00000000" + tempBinStr; - size = tempBinStr.length(); - } - } - return tempBinStr; - } + /** + * 涓嶈冻width涓瓧鑺傚搴︽椂锛屽墠闈㈣ˉ0鑷硍idth*8 + * + * @param value + * @param width + * @return + */ + public static String toBinary8String(int value, int width) { + String tempBinStr = toBinary8String(value); + int size = tempBinStr.length(); + for (int i = 0; i < width; i++) { + int temp = (i + 1) * 8; + if (temp > size) { + tempBinStr = "00000000" + tempBinStr; + size = tempBinStr.length(); + } + } + return tempBinStr; + } - /** - * 琛ヨ冻8浣� - * - * @param value - * @return - */ - public static String toBinary8String(int value) { - String temp = Integer.toBinaryString(value); - if (value == 0) { - temp = "00000000"; - } - int length = temp.length(); - while (length < 8) { - temp = "0" + temp; - length = temp.length(); - } - return temp; - } + /** + * 琛ヨ冻8浣� + * + * @param value + * @return + */ + public static String toBinary8String(int value) { + String temp = Integer.toBinaryString(value); + if (value == 0) { + temp = "00000000"; + } + int length = temp.length(); + while (length < 8) { + temp = "0" + temp; + length = temp.length(); + } + return temp; + } - public static String toEmptyBinaryWidthString(int width) { - String str = ""; - for (int i = 0; i < width; i++) { - str += "00000000"; - } - return str; - } + public static String toEmptyBinaryWidthString(int width) { + String str = ""; + for (int i = 0; i < width; i++) { + str += "00000000"; + } + return str; + } - public static byte[] emptyBytes(int bytes) { - byte[] bys = new byte[bytes]; - for (int i = 0; i < bytes; i++) { - bys[i] = emptyByte(); - } - return bys; - } + public static byte[] emptyBytes(int bytes) { + byte[] bys = new byte[bytes]; + for (int i = 0; i < bytes; i++) { + bys[i] = emptyByte(); + } + return bys; + } - public static byte emptyByte() { - Integer b0 = 0x00; - return b0.byteValue(); - } + public static byte emptyByte() { + Integer b0 = 0x00; + return b0.byteValue(); + } - public static byte binToBytes(String bin) { - return Integer.valueOf(bin, 2).byteValue(); - } + public static byte binToBytes(String bin) { + return Integer.valueOf(bin, 2).byteValue(); + } - public static byte[] binaryStrToBytes(String binStr) { - int size = binStr.length() / 8; - byte[] bs = new byte[size]; - for (int i = 0; i < size; i++) { - String temp = binStr.substring(i * 8, (i + 1) * 8); - bs[i] = Integer.valueOf(temp, 2).byteValue(); - } - return bs; - } + public static byte[] binaryStrToBytes(String binStr) { + int size = binStr.length() / 8; + byte[] bs = new byte[size]; + for (int i = 0; i < size; i++) { + String temp = binStr.substring(i * 8, (i + 1) * 8); + bs[i] = Integer.valueOf(temp, 2).byteValue(); + } + return bs; + } - /** - * 鍗佸叚杩涘埗涓茶浆瀛楄妭鏁扮粍 - * - * @param hexStr - * @return - */ - public static byte[] hexStrToBytes(String hexStr) { - int size = hexStr.length() / 2; - byte[] bytes = new byte[size]; - for (int i = 0; i < size; i++) { - String tmp = hexStr.substring(i * 2, (i + 1) * 2); - Integer tmpHex = Integer.parseInt(tmp, 16); - bytes[i] = tmpHex.byteValue(); - } - return bytes; - } + /** + * 鍗佸叚杩涘埗涓茶浆瀛楄妭鏁扮粍 + * + * @param hexStr + * @return + */ + public static byte[] hexStrToBytes(String hexStr) { + int size = hexStr.length() / 2; + byte[] bytes = new byte[size]; + for (int i = 0; i < size; i++) { + String tmp = hexStr.substring(i * 2, (i + 1) * 2); + Integer tmpHex = Integer.parseInt(tmp, 16); + bytes[i] = tmpHex.byteValue(); + } + return bytes; + } - /** - * 涓ゅ瓧鑺傞珮浣庝綅瑁呮崲 - * - * @param b - * @return - */ - public static byte[] hexHeightLow(byte[] b) { - byte[] b2 = new byte[2]; - // 杞崲楂樹綆浣� - b2[0] = b[1]; - b2[1] = b[0]; - return b2; - } + /** + * 涓ゅ瓧鑺傞珮浣庝綅瑁呮崲 + * + * @param b + * @return + */ + public static byte[] hexHeightLow(byte[] b) { + byte[] b2 = new byte[2]; + // 杞崲楂樹綆浣� + b2[0] = b[1]; + b2[1] = b[0]; + return b2; + } - /** - * 16杩涘埗杞崲涓�10杩涘埗 - * - * @param strHex - * @return - */ - public static int hexToInt(String strHex) { - short s = (short) (Integer.valueOf(strHex, 16) & 0xffff); - return s; - } + /** + * 16杩涘埗杞崲涓�10杩涘埗 + * + * @param strHex + * @return + */ + public static int hexToInt(String strHex) { + short s = (short) (Integer.valueOf(strHex, 16) & 0xffff); + return s; + } - /** - * 16杩涘埗杞崲涓�10杩涘埗 - * - * @param strHex - * @return - */ - public static int hexToBigInt(String strHex) { - BigInteger bigint = new BigInteger(strHex, 16); - return bigint.intValue(); - } + /** + * 16杩涘埗杞崲涓�10杩涘埗 + * + * @param strHex + * @return + */ + public static int hexToBigInt(String strHex) { + BigInteger bigint = new BigInteger(strHex, 16); + return bigint.intValue(); + } - /** - * int杞�16杩涘埗瀛楃涓� 2浣� 00 00 - * - * @param num - * @return - */ - public static String intToHexStr(int num) { - // 闇�瑕佷娇鐢�2瀛楄妭琛ㄧずb - return String.format("%04x", num).toUpperCase(); - } + /** + * int杞�16杩涘埗瀛楃涓� 2浣� 00 00 + * + * @param num + * @return + */ + public static String intToHexStr(int num) { + // 闇�瑕佷娇鐢�2瀛楄妭琛ㄧずb + return String.format("%04x", num).toUpperCase(); + } - public static void main(String[] args) { - - double d1 = hexToBigInt("000906EB")/100.0; - double d2 = hexToBigInt("0004F6E6")/100.0; - - System.out.println(d1); - System.out.println(d2); - - System.out.println(d1 + d2); + public static void main(String[] args) { - - } +// double d1 = hexToBigInt("000906EB")/100.0; +// double d2 = hexToBigInt("0004F6E6")/100.0; +// +// System.out.println(d1); +// System.out.println(d2); +// +// System.out.println(d1 + d2); - /** - * 16杩涘埗瀛楃涓� 楂樹綆鎹綅 - * - * @param info - * @return - */ - public static String tran_LH(String info) { - return info.substring(2) + info.substring(0, 2); - } - /** - * 16杩涘埗瀛楃涓� 楂樹綆鎹綅 8涓瓧绗� - * - * @param info - * @return - */ - public static String tran_LH8(String info) { - return tran_LH(info.substring(4)) + tran_LH(info.substring(0, 4)); - } + String kyeNumBin = "10100100"; + String key = "00000" + kyeNumBin.substring(0, 3); - /** - * 灏� 4瀛楄妭鐨�16杩涘埗瀛楃涓诧紝杞崲涓�32浣嶅甫绗﹀彿鐨勫崄杩涘埗娴偣鍨� - * - * 42c60000 -> 99.00 - * - * @param str - * 4瀛楄妭 16杩涘埗瀛楃 - * @return - */ - public static float hexToFloat(String str) { - return Float.intBitsToFloat(new BigInteger(str, 16).intValue()); - } + System.out.println("-----key-----" + key); - /** - * 灏嗗甫绗﹀彿鐨�32浣嶆诞鐐规暟瑁呮崲涓�16杩涘埗 - * - * 99.00 -> 42c60000 - * - * - * @param value - * @return - */ - public static String folatToHexString(Float value) { - return Integer.toHexString(Float.floatToIntBits(value)); - } + String bin2hex = BytesUtil.bin2Hex(key); - /** - * int杞�16杩涘埗瀛楃涓� 1浣嶇疆 00 - * - * @param num - * @return - */ - public static String intToHexStr1(int num) { - // 闇�瑕佷娇鐢�2瀛楄妭琛ㄧずb - return String.format("%02x", num).toUpperCase(); - } + System.out.println("-----bin2hex-----" + bin2hex); - public static String hexString2binaryString(String hexString, int num) { - //16杩涘埗杞�10杩涘埗 - BigInteger sint = new BigInteger(hexString, num); - //10杩涘埗杞�2杩涘埗 - String str = sint.toString(2); - if(str.length() < num){ - for (int i = str.length(); i < num; i++) { - str = "0" + str; - } - } - return str; - } + int keyValue = BytesUtil.hexToInt(bin2hex); - /** - * 灏嗕簩杩涘埗杞崲涓�10杩涘埗 - * @param binStr - * @return - */ - public static Integer biannary2Decimal(String binStr){ + System.out.println("-----keyValue-----" + keyValue); - Integer sum = 0; - int len = binStr.length(); + key = "0000" + kyeNumBin.substring(4); - for (int i=1;i<=len;i++){ - //绗琲浣� 鐨勬暟瀛椾负锛� - int dt = Integer.parseInt(binStr.substring(i-1,i)); - sum+=(int)Math.pow(2,len-i)*dt; - } - return sum; - } + System.out.println("-----key-----" + key); - /** - * 浜岃繘鍒惰ˉ鐮侊細鍙栧弽鍙e姞1 - * @param str - * @return - */ - public static Integer twoToString(String str) { - String two = ""; - String[] split = str.split(""); - for (int i = 0;i< split.length;i++){ - if("1".equals(split[i])){ - two += "0"; - }else if("0".equals(split[i])){ - two += "1"; - } - } - return biannary2Decimal(two) + 1; - } + bin2hex = BytesUtil.bin2Hex(key); + System.out.println("-----bin2hex-----" + bin2hex); + keyValue = BytesUtil.hexToInt(bin2hex); + System.out.println("-----keyValue-----" + keyValue); - public static String byteToHex(byte b) { - String hex = Integer.toHexString(b & 0xFF); - if (hex.length() < 2) { - hex = "0" + hex; - } - return hex; - } + } + + /** + * 16杩涘埗瀛楃涓� 楂樹綆鎹綅 + * + * @param info + * @return + */ + public static String tran_LH(String info) { + return info.substring(2) + info.substring(0, 2); + } + + /** + * 16杩涘埗瀛楃涓� 楂樹綆鎹綅 8涓瓧绗� + * + * @param info + * @return + */ + public static String tran_LH8(String info) { + return tran_LH(info.substring(4)) + tran_LH(info.substring(0, 4)); + } + + /** + * 灏� 4瀛楄妭鐨�16杩涘埗瀛楃涓诧紝杞崲涓�32浣嶅甫绗﹀彿鐨勫崄杩涘埗娴偣鍨� + * <p> + * 42c60000 -> 99.00 + * + * @param str 4瀛楄妭 16杩涘埗瀛楃 + * @return + */ + public static float hexToFloat(String str) { + return Float.intBitsToFloat(new BigInteger(str, 16).intValue()); + } + + /** + * 灏嗗甫绗﹀彿鐨�32浣嶆诞鐐规暟瑁呮崲涓�16杩涘埗 + * <p> + * 99.00 -> 42c60000 + * + * @param value + * @return + */ + public static String folatToHexString(Float value) { + return Integer.toHexString(Float.floatToIntBits(value)); + } + + /** + * int杞�16杩涘埗瀛楃涓� 1浣嶇疆 00 + * + * @param num + * @return + */ + public static String intToHexStr1(int num) { + // 闇�瑕佷娇鐢�2瀛楄妭琛ㄧずb + return String.format("%02x", num).toUpperCase(); + } + + public static String hexString2binaryString(String hexString, int num) { + //16杩涘埗杞�10杩涘埗 + BigInteger sint = new BigInteger(hexString, num); + //10杩涘埗杞�2杩涘埗 + String str = sint.toString(2); + if (str.length() < num) { + for (int i = str.length(); i < num; i++) { + str = "0" + str; + } + } + return str; + } + + /** + * 灏嗕簩杩涘埗杞崲涓�10杩涘埗 + * + * @param binStr + * @return + */ + public static Integer biannary2Decimal(String binStr) { + + Integer sum = 0; + int len = binStr.length(); + + for (int i = 1; i <= len; i++) { + //绗琲浣� 鐨勬暟瀛椾负锛� + int dt = Integer.parseInt(binStr.substring(i - 1, i)); + sum += (int) Math.pow(2, len - i) * dt; + } + return sum; + } + + /** + * 浜岃繘鍒惰ˉ鐮侊細鍙栧弽鍙e姞1 + * + * @param str + * @return + */ + public static Integer twoToString(String str) { + String two = ""; + String[] split = str.split(""); + for (int i = 0; i < split.length; i++) { + if ("1".equals(split[i])) { + two += "0"; + } else if ("0".equals(split[i])) { + two += "1"; + } + } + return biannary2Decimal(two) + 1; + } + + public static String byteToHex(byte b) { + String hex = Integer.toHexString(b & 0xFF); + if (hex.length() < 2) { + hex = "0" + hex; + } + return hex; + } } -- Gitblit v1.9.3