content: package com.vi.vioserial.util;
  
  /**
   * 数据转换工具类
   * Data conversion tool class
   *
   * @author Vi
   * @date 2019-07-17 17:43
   * @e-mail cfop_f2l@163.com
   */
  
  public class SerialDataUtils {
  
      /**
       * 判断奇数或偶数，位运算，最后一位是1则为奇数，为0是偶数
       * Judging odd or even numbers, bit operations, the last bit is 1 is odd, 0 is even
       */
      public static int isOdd(int num) {
          return num & 1;
      }
  
      /**
       * Hex字符串转int
       * Hex string to int
       */
      public static int HexToInt(String inHex) {
          return Integer.parseInt(inHex, 16);
      }
  
      /**
       * Hex字符串转byte
       * Hex string to byte
       */
      public static byte HexToByte(String inHex) {
          return (byte) Integer.parseInt(inHex, 16);
      }
  
      /**
       * 1字节转2个Hex字符
       * 1 byte to 2 Hex characters
       */
      public static String Byte2Hex(Byte inByte) {
          return String.format("%02x", new Object[]{inByte}).toUpperCase();
      }
  
      /**
       * 字节数组转转hex字符串
       * Byte array to hex string
       */
      public static String ByteArrToHex(byte[] inBytArr) {
          StringBuilder strBuilder = new StringBuilder();
          for (byte valueOf : inBytArr) {
              strBuilder.append(Byte2Hex(Byte.valueOf(valueOf)));
              strBuilder.append(" ");
          }
          return strBuilder.toString();
      }
  
      /**
       * 字节数组转转hex字符串，可选长度
       * Byte array to hex string, optional length
       */
      public static String ByteArrToHex(byte[] inBytArr, int offset, int byteCount) {
          StringBuilder strBuilder = new StringBuilder();
          int j = byteCount;
          for (int i = offset; i < j; i++) {
              strBuilder.append(Byte2Hex(Byte.valueOf(inBytArr[i])));
          }
          return strBuilder.toString();
      }
  
      /**
       * hex字符串转字节数组
       * Hex string to byte array
       */
      public static byte[] HexToByteArr(String inHex) {
          byte[] result;
          int hexlen = inHex.length();
          if (isOdd(hexlen) == 1) {
              hexlen++;
              result = new byte[(hexlen / 2)];
              inHex = "0" + inHex;
          } else {
              result = new byte[(hexlen / 2)];
          }
          int j = 0;
          for (int i = 0; i < hexlen; i += 2) {
              result[j] = HexToByte(inHex.substring(i, i + 2));
              j++;
          }
          return result;
      }
  
      /**
       * hex字符串转字节数组
       * Hex string to byte array
       */
      public static byte[] HexToByteArr2(String inHex) {
          byte[] result;
          int hexlen = inHex.length();
          if (isOdd(hexlen) == 1) {
              hexlen++;
              result = new byte[(hexlen / 2)];
              inHex = "0" + inHex;
          } else {
              result = new byte[(hexlen / 2)];
          }
          int j = 0;
          for (int i = 0; i < hexlen; i += 2) {
              result[j] = HexToByte(inHex.substring(i, i + 2));
              j++;
          }
          return result;
      }
  
      /**
       * 字符串转换为16进制字符串
       * String to a hex string
       *
       * @param s
       * @return
       */
      public static String stringToHexString(String s) {
          String str = "";
          for (int i = 0; i < s.length(); i++) {
              int ch = (int) s.charAt(i);
              String s4 = Integer.toHexString(ch);
              str = str + s4;
          }
          return str;
      }
  
      /**
       * 16进制字符串转换为字符串
       * Convert a hex string to a string
       *
       * @param s
       * @return
       */
      public static String hexStringToString(String s) {
          if (s == null || s.equals("")) {
              return null;
          }
          s = s.replaceAll(" ", "");
          byte[] baKeyword = new byte[s.length() / 2];
          for (int i = 0; i < baKeyword.length; i++) {
              try {
                  baKeyword[i] = (byte) (0xff & Integer.parseInt(
                          s.substring(i * 2, i * 2 + 2), 16));
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
          try {
              s = new String(baKeyword, "gbk");
          } catch (Exception e1) {
              e1.printStackTrace();
          }
          return s;
      }
  }
  