java byte 16进制字符串相关操作

这篇博客详细介绍了Java中如何进行16进制字符串与2进制之间的转换,并探讨了如何查找byte数组中连续重复的部分,包括16进制字符串转2进制、2进制转16进制字符串以及查找重复连续位置的方法。
16进制字符串转2进制
2进制转16进制字符串
查找byte数组中存在的重复连续的位置第一个


public class Main1 {
   public static void main(String[] args) {
       System.out.println("Hello world! ");


   }


   /**
    * 查找byte数组中存在的重复连续的位置第一个
    *
    * @param source {0x01,0x02,0x03}
    * @param dest   {0x01,0x02}
    * @return 0
    */
   public static int indexOf(byte[] source, byte[] dest) {
       if (null == source || source.length <= 0 || null == dest || dest.length <= 0) {
           return -1;
       }
       int sourceLen = source.length;
       int destLen = dest.length;
       if (sourceLen < destLen) {
           return -1;
       }
       for (int i = 0; i <= sourceLen - destLen; i++) {
           int index = i;
           for (int j = 0; j < destLen; j++) {
               byte bt = source[j + i];
               if (bt != dest[j]) {
                   index = -1;
                   break;
               }
           }
           if (index != -1) {
               return index;
           }
       }
       return -1;

   }

   /**
    * 求和
    *
    * @param buff
    * @return
    */
   public static int sumByte(byte[] buff) {
       int sum = 0;
       if (null == buff || buff.length <= 0) {
           return sum;
       }

       for (byte b : buff) {
           sum += byte2int(b);
       }
       return sum & 0xFF;
   }

   /**
    * byte转int
    *
    * @param b
    * @return
    */
   public static int byte2int(byte b) {
       return b < 0 ? 256 + b : b;
   }

   public static String byte2hexStr(byte b) {
       String s = Integer.toHexString(0xFF & b);
       if (s.length() < 2) {
           s = "0" + s;
       }
       return s.toUpperCase();
   }

   /**
    * 2进制转16进制字符串
    *
    * @param b
    * @return
    */
   public static String byte2hexStr(byte[] b) {
       if (null == b || b.length <= 0) {
           return "";
       }
       StringBuilder sb = new StringBuilder(b.length);
       for (byte value : b) {
           sb.append(byte2hexStr(value));
       }
       return sb.toString();
   }

   /**
    * 字符串转byte数组
    *
    * @param hex
    * @return
    */
   public static byte[] hexStr2byte(String hex) {
       byte[] b = null;
       if (null == hex || hex.length() <= 0) {
           return b;
       }
       hex = hex.toUpperCase();
       int len = hex.length() / 2;

       char[] hexChars = hex.toCharArray();
       b = new byte[len];
       for (int i = 0; i < len; i++) {
           int pos = i * 2;
           b[i] = (byte) (char2byte(hexChars[pos]) << 4 | char2byte(hexChars[pos + 1]));
       }

       return b;
   }

   /**
    * char 转byte
    *
    * @param c
    * @return
    */
   public static byte char2byte(char c) {
       return (byte) "0123456789ABCDEF".indexOf(c);
   }

   /**
    * 两个字节表示数据长度
    *
    * @param len 255
    * @return 00FF
    */
   public static byte[] setLen(int len) {
       byte[] buff = new byte[2];
       buff[0] = (byte) (len / 256);
       buff[1] = (byte) (len % 256);
       return buff;
   }

   /**
    * 解析两位的数据长度 00FF
    *
    * @param l 00
    * @param r FF
    * @return 255
    */
   public static int getLen(byte l, byte r) {
       return byte2int(l) * 256 + byte2int(r);
   }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值