|
@@ -0,0 +1,78 @@
|
|
|
+package com.warewms.hailiang.util;
|
|
|
+
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created with IntelliJ IDEA.
|
|
|
+ *
|
|
|
+ * @author: liuzhifei
|
|
|
+ * Date: 2023/8/10
|
|
|
+ * Time: 10:34
|
|
|
+ * To change this template use File | Settings | File Templates.
|
|
|
+ * Description: 消息解析工具
|
|
|
+ **/
|
|
|
+public class ParseMsgTools {
|
|
|
+ /**
|
|
|
+ * 功能描述: 16进制字符串转字节数组
|
|
|
+ * @param src 16进制字符串
|
|
|
+ * @return byte[]
|
|
|
+ */
|
|
|
+ public static byte[] hexString2Bytes(String src) {
|
|
|
+ int l = src.length() / 2;
|
|
|
+ byte[] ret = new byte[l];
|
|
|
+ for (int i = 0; i < l; i++) {
|
|
|
+ ret[i] = (byte) Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字符串转16进制字符串
|
|
|
+ * @param str
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String StringTohexString(String str){
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ //将字符串转换为字符数组
|
|
|
+ char ch[] = str.toCharArray();
|
|
|
+ for(int i = 0; i < ch. length; i++) {
|
|
|
+ String hexString = Integer.toHexString(ch[i]);
|
|
|
+ sb.append(hexString);
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+ public static void main(String[] args) throws UnsupportedEncodingException {
|
|
|
+// String str = "23";
|
|
|
+// StringBuffer sb = new StringBuffer();
|
|
|
+// //将字符串转换为字符数组
|
|
|
+// char ch[] = str.toCharArray();
|
|
|
+// for(int i = 0; i < ch. length; i++) {
|
|
|
+// String hexString = Integer.toHexString(ch[i]);
|
|
|
+// sb.append(hexString);
|
|
|
+// }
|
|
|
+// String result = sb.toString();
|
|
|
+
|
|
|
+// System.out.println(result);
|
|
|
+// byte[] bytes = new byte[37];
|
|
|
+// bytes[0] = 00;
|
|
|
+// String s ="E800230100063132333332310200173131313131313131313131313131313131313131313131";
|
|
|
+
|
|
|
+// String s ="E8 00 23 01 00 06 31 32 33 33 32 31 02 00 17 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31";
|
|
|
+// String[] s2 = s.split(" ");
|
|
|
+// int i =Integer.parseInt(s2[0],16) ^ Integer.parseInt(s2[1],16) ;
|
|
|
+// for (int i1 = 1; i1 < s2.length-1; i1++) {
|
|
|
+// i = i ^ Integer.parseInt(s2[i1+1],16);
|
|
|
+// }
|
|
|
+// System.out.println(Integer.toHexString(i));
|
|
|
+
|
|
|
+// System.out.println(Integer.parseInt("23",16));
|
|
|
+// System.out.println(Arrays.toString(hexString2Bytes("8000")));
|
|
|
+
|
|
|
+// System.out.println(Integer.parseInt("E8",16));
|
|
|
+// for (int i = 1; i < bytes.length -1; i++) {
|
|
|
+// y = y ^ bytes[i + 1];
|
|
|
+// System.out.println(y);
|
|
|
+// }
|
|
|
+// System.out.println(Integer.toHexString(y));
|
|
|
+ }
|
|
|
+}
|