ByteUtil工具类 java

可以和Base58等算法工具类配合使用

public class ByteUtil { 

      /**
       * Remove leading 0x00's from a byte array. 
       */ 
      public static byte[] stripLeadingNullBytes(byte[] input) { 
        byte[] result = Arrays.copyOf(input, input.length); 
        while (result.length > 0 && result[0] == 0x00) { 
          result = Arrays.copyOfRange(result, 1, result.length); 
        } 
        return result; 
      } 

      static final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray(); 

      /**
       * Convert a byte array into its hex string equivalent. 
       */ 
      public static String toHexString(byte[] data) { 
        char[] chars = new char[data.length * 2]; 
        for (int i = 0; i < data.length; i++) { 
          chars[i * 2] = HEX_DIGITS[(data[i] >> 4) & 0xf]; 
          chars[i * 2 + 1] = HEX_DIGITS[data[i] & 0xf]; 
        } 
        return new String(chars).toLowerCase(); 
      } 

      /**
       * Convert a hex string into its byte array equivalent. 
       */ 
      public static byte[] toByteArray(String data) { 
        if (data == null) { 
          return new byte[] {}; 
        } 

        if (data.length() == 0) { 
          return new byte[] {}; 
        } 

        while (data.length() < 2) { 
          data = "0" + data; 
        } 

        if (data.substring(0, 2).toLowerCase().equals("0x")) { 
          data = data.substring(2); 
        } 
        if (data.length() % 2 == 1) { 
          data = "0" + data; 
        } 

        data = data.toUpperCase(); 

        byte[] bytes = new byte[data.length() / 2]; 
        String hexString = new String(HEX_DIGITS); 
        for (int i = 0; i < bytes.length; i++) { 
          int byteConv = hexString.indexOf(data.charAt(i * 2)) * 0x10; 
          byteConv += hexString.indexOf(data.charAt(i * 2 + 1)); 
          bytes[i] = (byte) (byteConv & 0xFF); 
        } 

        return bytes; 
      } 

      /**
       * Reverse the endian-ness of a byte array. 
       *  
       * @param data Byte array to flip 
       * @return Flipped array 
       */ 
      public static byte[] flipEndian(byte[] data) { 
        if (data == null) { 
          return new byte[0]; 
        } 
        byte[] newData = new byte[data.length]; 
        for (int i = 0; i < data.length; i++) { 
          newData[data.length - i - 1] = data[i]; 
        } 

        return newData; 
      } 

      /**
       * Pad a byte array with leading bytes. 
       *  
       * @param data Data that needs padding 
       * @param size The final desired size of the data. 
       * @param pad The byte value to use in padding the data. 
       * @return A padded array. 
       */ 
      public static byte[] leftPad(byte[] data, int size, byte pad) { 
        if (size <= data.length) { 
          return data; 
        } 

        byte[] newData = new byte[size]; 
        for (int i = 0; i < size; i++) { 
          newData[i] = pad; 
        } 
        for (int i = 0; i < data.length; i++) { 
          newData[size - i - 1] = data[data.length - i - 1]; 
        } 

        return newData; 
      } 

      /**
       * Reads a section of a byte array and returns it as its own byte array, not unlike a substring. 
       *  
       * @param data Byte array to read from. 
       * @param start Starting position of the desired data. 
       * @param size Size of the data. 
       * @return Byte array containing the desired data. 
       */ 
      public static byte[] readBytes(byte[] data, int start, int size) { 
        if (data.length < start + size) { 
          return new byte[0]; 
        } 

        byte[] newData = Arrays.copyOfRange(data, start, start + size); 

        return newData; 
      } 
    }
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值