Byte[] 转换 截取等操作

1、工作中用到的与Byte[]相关方法工具类。记录一下

包含:16进制字符串转换Byte[]、Byte[]转换成16进制字符、字符串转换成十六进制字符串、16进制直接转换成为字符串(无需Unicode解码)、校验和、Byte[]截取指定长度数组、BCD码转为10进制串(阿拉伯数据)、int与byte 互转、byte[]转int高低位互换、

byte[]转int 、byte[]与short 互转、10进制串转为BCD码 

     /**
      * 把16进制字符串转换成字节数组
      * @param hexString
      * @return byte[]
      */
	  public static byte[] hexStringToByte(String hex) {
          if ((hex == null) || (hex.equals(""))){
              return null;
          }
          else if (hex.length()%2 != 0){
              return null;
          }
          else{                
              hex = hex.toUpperCase();
              int len = hex.length()/2;
              byte[] b = new byte[len];
              char[] hc = hex.toCharArray();
              for (int i=0; i<len; i++){
                  int p=2*i;
                  b[i] = (byte) (charToByte(hc[p]) << 4 | charToByte(hc[p+1]));
              }
            return b;
          }           

	  }
	  
	  private static int charToByte(char c) {
		   byte b = (byte) "0123456789ABCDEF".indexOf(c);
		   return b;
	  }

	  /**
	   * 字节数组转换成16进制字符
	   * @param hint
	   * @param b
	   * @return
	   */
	  public static  String printHexString(String hint, byte[] b) {
			String str = "";
			int size=b.length;
			for (int i = 0; i < size; i++) {
				String hex = Integer.toHexString(b[i] & 0xFF);
				if (hex.length() == 1) {
					hex = '0' + hex;
				}
				str += hex.toUpperCase();
			}
			return hint+ str;
	  }
	 
	  /** 
	    * 字符串转换成十六进制字符串
	    */  
	  public static String str2HexStr(String str) {  
	      char[] chars = "0123456789ABCDEF".toCharArray();  
	      StringBuilder sb = new StringBuilder("");
	      byte[] bs = str.getBytes();  
	      int bit;  
	      for (int i = 0; i < bs.length; i++) {  
	          bit = (bs[i] & 0x0f0) >> 4;  
	          sb.append(chars[bit]);  
	          bit = bs[i] & 0x0f;  
	          sb.append(chars[bit]);  
	      }  
	      return sb.toString();  
	  } 
	 
	  
	  /**
	   * 16进制直接转换成为字符串(无需Unicode解码)
	   * @param hexStr
	   * @return
	   */
	  public static String hexStr2Str(String hexStr) {
	      String str = "0123456789ABCDEF";
	      char[] hexs = hexStr.toCharArray();
	      byte[] bytes = new byte[hexStr.length() / 2];
	      int n;
	      for (int i = 0; i < bytes.length; i++) {
	          n = str.indexOf(hexs[2 * i]) * 16;
	          n += str.indexOf(hexs[2 * i + 1]);
	          bytes[i] = (byte) (n & 0xff);
	      }
	      return new String(bytes);
	  }
 
	 /**
	  * 校验和
	  * @param data
	  * @return
	  */
	 public static String makeChecksum(String data) {
			 if (data == null || data.equals("")) {
			  return "";
			 }
			 int total = 0;
			 int len = data.length();
			 int num = 0;
			 while (num < len) {
			  String s = data.substring(num, num + 2);
			  total += Integer.parseInt(s, 16);
			  num = num + 2;
			 }
			 /**
			  * 用256求余最大是255,即16进制的FF
			  */
			 int mod = total % 256;
			 String hex = Integer.toHexString(mod);
			 len = hex.length();
			 // 如果不够校验位的长度,补0,这里用的是两位校验
			 if (len < 2) {
			  hex = "0" + hex;
			 }
			 return hex.toUpperCase();
	  }

	 /**
	  * 截取指定长度数组
	  * @param src
	  * @param begin
	  * @param count
	  * @return
	  */
	 public static byte[] subBytes(byte[] src, int begin, int count) {  
		    byte[] bs = new byte[count];  
		    System.arraycopy(src, begin, bs, 0, count);  
		    return bs;  
	}  
	 
	
	/**
	 * @功能: BCD码转为10进制串(阿拉伯数据)
	 * @参数: BCD码
	 * @结果: 10进制串
	 */
	public static String bcd2Str(byte[] bytes) {
		StringBuffer temp = new StringBuffer(bytes.length * 2);
		for (int i = 0; i < bytes.length; i++) {
			temp.append((byte) ((bytes[i] & 0xf0) >>> 4));
			temp.append((byte) (bytes[i] & 0x0f));
		}
		return temp.toString().substring(0, 1).equalsIgnoreCase("0") ? temp
				.toString().substring(1) : temp.toString();
	}
	 
	 
     /** 
      * int转byte 
      * @param x 
      * @return 
      */  
    public static byte intToByte(int x) {    
        return (byte) x;    
    }    
    /** 
     * byte转int 
     * @param b 
     * @return 
     */  
    public static int byteToInt(byte b) {    
        //Java的byte是有符号,通过 &0xFF转为无符号  
        return b & 0xFF;    
    }    
        
    /** 
     * byte[]转int 
     * @param b 
     * @return 
     */  
    public static int byteArrayToInt(byte[] b) {    
        return   b[3] & 0xFF |    
                (b[2] & 0xFF) << 8 |    
                (b[1] & 0xFF) << 16 |    
                (b[0] & 0xFF) << 24;    
    }  
    public static int byteArrayToInt(byte[] b, int index){  
          return   b[index+3] & 0xFF |    
                  (b[index+2] & 0xFF) << 8 |    
                  (b[index+1] & 0xFF) << 16 |    
                  (b[index+0] & 0xFF) << 24;    
    }  
    
    /**
     * byte[]转int高低位互换
     * @param b
     * @return
     */
    public static int highLowExchangeToInt(byte[] b) {
    	return b[0] & 0xFF | (b[1] & 0xFF) << 8;
    }
    
    /**
     * 将byte数组转int
     * 正常情况下4个byte可以转换成一个int
     * 当数组不足4只需要高位补上就行
     * @param b
     * @return
     */
    public static int byteArrToInt(byte[] b) {  
    	int s =0;
    	if(b.length == 1) {
    		s = b[0] & 0xFF;
    	}else if(b.length == 2) {
    		s = b[1] & 0xFF | (b[0] & 0xFF) << 8 ;
    	}else if(b.length ==3) {
    		s = b[2] & 0xFF | (b[1] & 0xFF) << 8 | (b[0] & 0xFF) << 16 ;
    	}else {
    		s = b[3] & 0xFF | (b[2] & 0xFF) << 8 | (b[1] & 0xFF) << 16 | (b[0] & 0xFF) << 24;  
    	}
        return  s;  
    }  
    
    /** 
     * int转byte[] 
     * @param a 
     * @return 
     */  
    public static byte[] intToByteArray(int a) {    
        return new byte[] {    
            (byte) ((a >> 24) & 0xFF),    
            (byte) ((a >> 16) & 0xFF),       
            (byte) ((a >> 8) & 0xFF),       
            (byte) (a & 0xFF)    
        };    
    }    
    /**  
     * short转byte[] 
     *   
     * @param b  
     * @param s  
     * @param index  
     */    
    public static void byteArrToShort(byte b[], short s, int index) {    
        b[index + 1] = (byte) (s >> 8);    
        b[index + 0] = (byte) (s >> 0);    
    }  
    /**  
     * byte[]转short  
     *   
     * @param b  
     * @param index  
     * @return  
     */    
    public static short byteArrToShort(byte[] b, int index) {    
        return (short) (((b[index + 0] << 8) | b[index + 1] & 0xff));    
    }  
    /**  
     * 16位short转byte[]  
     *   
     * @param s  
     *            short  
     * @return byte[] 
     * */    
    public static byte[] shortToByteArr(short s) {    
        byte[] targets = new byte[2];    
        for (int i = 0; i < 2; i++) {    
            int offset = (targets.length - 1 - i) * 8;    
            targets[i] = (byte) ((s >>> offset) & 0xff);    
        }    
        return targets;    
    }  
    /** 
     * byte[]转16位short 
     * @param b 
     * @return 
     */  
    public static short byteArrToShort(byte[] b){  
        return byteArrToShort(b,0);  
    }  
      
      
    /** 
     * 流转换为byte[] 
     * @param inStream 
     * @return 
     */  
    public static byte[] readInputStream(InputStream inStream) {  
        ByteArrayOutputStream outStream = null;  
        try {  
            outStream = new ByteArrayOutputStream();  
            byte[] buffer = new byte[1024];  
            byte[] data = null;  
            int len = 0;  
            while ((len = inStream.read(buffer)) != -1) {  
                outStream.write(buffer, 0, len);  
            }  
            data = outStream.toByteArray();  
            return data;  
        }catch (IOException e) {  
            return null;  
        } finally {  
            try {  
                if (outStream != null) {  
                    outStream.close();  
                }  
                if (inStream != null) {  
                    inStream.close();  
                }  
            } catch (IOException e) {  
                return null;  
            }  
        }  
    }  
    /** 
     * byte[]转inputstream 
     * @param b 
     * @return 
     */  
    public static InputStream readByteArr(byte[] b){  
        return new ByteArrayInputStream(b);  
    }  


    /** 
     * 字节数组转16进制字符串 
     * @param b 
     * @return 
     */  
    public static String byteArrToHexString(byte[] b){  
          String result="";  
          for (int i=0; i < b.length; i++) {  
            result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring(1);  
          }  
          return result;  
    }  
      
    /** 
     * 16进制字符创转int 
     * @param hexString 
     * @return 
     */  
    public static int hexStringToInt(String hexString){  
        return Integer.parseInt(hexString,16);  
    }  
    /** 
     * 十进制转二进制 
     * @param i 
     * @return 
     */  
    public static String intToBinary(int i){  
        return Integer.toBinaryString(i);  
    }  
    

    /**
     * int转16进制
     * @param b
     * @return
     */
    public static String intToHex16(int b) {
            return String.format("%02x", b);
    }
    
    /**
     * int转hex 长度不够前面补零
     * @param b
     * @param length
     * @return
     */
    public static String intToHexByLength(int b,int byteLength) {
    	int length = byteLength*2;
    	String hex = intToHex16(b);
    	hex = hex.length() <length ? "00"+hex :hex; 
    	return hex;
    }
    
    
    /**
     * 高低位互换高位在后低位前
     * @param dec
     * @return16进制字符串
     */
    public static String decToHex(int dec) {
        String hex = "";
        while(dec != 0) {
            String h = Integer.toString(dec & 0xff, 16);
            if((h.length() & 0x01) == 1)
                h = '0' + h;
            hex = hex + h;
            dec = dec >> 8;
        }
        hex = hex.length()<4?hex+"00":hex;
        return hex;
    }

	
	/** 
	    * @函数功能: 10进制串转为BCD码 
	    * @输入参数: 10进制串 
	    * @输出结果: BCD码 
	    */  
	public static byte[] str2Bcd(String asc) {  
	    int len = asc.length();  
	    int mod = len % 2;  
	   
	    if (mod != 0) {  
	     asc = "0" + asc;  
	     len = asc.length();  
	    }  
	   
	    byte abt[] = new byte[len];  
	    if (len >= 2) {  
	     len = len / 2;  
	    }  
	   
	    byte bbt[] = new byte[len];  
	    abt = asc.getBytes();  
	    int j, k;  
	   
	    for (int p = 0; p < asc.length()/2; p++) {  
	     if ( (abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {  
	      j = abt[2 * p] - '0';  
	     } else if ( (abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {  
	      j = abt[2 * p] - 'a' + 0x0a;  
	     } else {  
	      j = abt[2 * p] - 'A' + 0x0a;  
	     }  
	   
	     if ( (abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {  
	      k = abt[2 * p + 1] - '0';  
	     } else if ( (abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {  
	      k = abt[2 * p + 1] - 'a' + 0x0a;  
	     }else {  
	      k = abt[2 * p + 1] - 'A' + 0x0a;  
	     }  
	   
	     int a = (j << 4) + k;  
	     byte b = (byte) a;  
	     bbt[p] = b;  
	    }  
	    return bbt;  
	}  
	

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值