`
yaodaqing
  • 浏览: 345686 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

byte--bytes--hex

阅读更多
package com.relonger.cclj.weight;

public final class BytesUtils {
	
	/**
	 * 二进制字符串转二进制数组,中间要用逗号隔开
	 * 只能处理无符号的数值
	 * 例如:00111011,01111111都可以处理,如果01111111二进制书中的第一位是1,则会报错
	 * @param b
	 * @return
	 */
	public static byte[] bytesStringToBytes(String b){
		if(b.length()<0){
			return null;
		}
		String[] in = b.split(",");
		byte[] by = new byte[in.length];
		for (int i = 0; i < in.length; i++) {
			by[i] = Byte.parseByte(in[i],2);
		}
		return by;
	}

	/**
	 * 二进制字符串,转十六进制字符串,中间要用逗号隔开
	 * 只能处理无符号的数值
	 * 例如:00111011,01111111都可以处理,如果01111111二进制书中的第一位是1,则会报错
	 */
	public static String bytesStringToHexString(String byteString){
		if(byteString.length()<0){
			return null;
		}
		String[] inputs = byteString.split(",");
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < inputs.length; i++) {
			byte[] b = new byte[1];
			b[0] = Byte.parseByte(inputs[i],2);
			sb.append(BytesUtils.bytesToHexString(b));
		}
		return sb.toString();
	}
	
	/**
	 * 二进制数组转二进制字符串
	 * @param b
	 * @return
	 */
	public static String bytesToBytesString(byte[] b){
		StringBuffer sb = new StringBuffer();
		String s ="";
		for(byte bs:b){  
			String sj = Integer.toBinaryString(bs);
		    s += sj;
		    int i = sj.length();
		    if (i<8) {	//8位不够,前面补零操作
				int in = 8-i;
				s = addZeroHead(s,in);
				sb.append(s);
				s="";
			}
		 }
		return sb.toString();
	}
	
	/**
	 * 前补零操作
	 * 二进制字符串中,不够八位
	 * @return
	 */
	public static String addZeroHead(String src,int addZero){
		String sr = src;
		String s = "";
		for(int f=0;f<addZero;f++){
			s += "0";
		}
		return sr = s+sr;
	}

	/**
		 * 二进制数组转十六进制字符串<br/>
		 * @param b
		 * @return String
		 */
		public static String bytesToHexString(byte[] b){
			if(b==null){
				return null;
			}
	        StringBuffer sb = new StringBuffer();   
	        for (int i = 0; i < b.length; i++) {   
	             String strHex=Integer.toHexString(b[i]);   
	             if(strHex.length() > 3){   
	                    sb.append(strHex.substring(6));   
	             } else {   
	                  if(strHex.length() < 2){   
	                     sb.append("0" + strHex);   
	                  } else {   
	                     sb.append(strHex);   
	                  }   
	             }   
	        }   
	       return  sb.toString();   
	   }

	/**
	 * 二进制字符串,转十六进制字符串
	 * 只能处理无符号的数值
	 * 例如:00111011,01111111都可以处理,如果01111111二进制书中的第一位是1,则会报错
	 */
	public static String hexStringToBytesString(String hexString){
		if(hexString.length()<0){
			return null;
		}
		String[] inputs = hexString.split(",");
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < inputs.length; i++) {
			byte[] b = new byte[1];
			b[0] = Byte.parseByte(inputs[i],2);
			sb.append(BytesUtils.bytesToHexString(b));
		}
		return sb.toString();
	}
	
	/**
	 * 十六进制字符串转二进制数组<br/>
	 * @param s
	 * @return
	 */
	public static byte[] hexStringToBytes(String s){
		if (s == null || s.equals("")) {
			return null;
		}
		s = s.toUpperCase();
		int length = s.length() / 2;
		char[] hexChars = s.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}
	
	/**
	 * 字符转为byte<br/>
	 * 把一个字符转成二进制<br/>
	 * @param c
	 * @return
	 */
	private static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}

	
	/**
	 * int to bytes<br/>
	 * 十进制转二进制数组;产生的数据在高<br/>
	 * @param i
	 * @return
	 */
	public static byte[] intToBytes(int i) { 
		byte[] b = new byte[4]; 
		b[0] = (byte) (0xff & i); 
		b[1] = (byte) ((0xff00 & i) >> 8); 
		b[2] = (byte) ((0xff0000 & i) >> 16); 
		b[3] = (byte) ((0xff000000 & i) >> 24); 
		return b; 
	}
	
	/**
	 * bytes to int  ;产生的int数据在高位<br/>
	 * 二进制数组转十进制,数组必须大于4,小于4会出错<br/>
	 * @param b
	 * @return
	 */
	public  static int bytesToInt(byte[] b) { 
		if(b.length<4){
			return 0;
		}
		int n = b[0] & 0xFF; 
		n |= ((b[1] << 8) & 0xFF00); 
		n |= ((b[2] << 16) & 0xFF0000); 
		n |= ((b[3] << 24) & 0xFF000000); 
		return n;
	}
	
	/**  
     * 合并两个byte数组  <br/>
     * @param src  合并在前
     * @param des  合并在后
     * @return  
     */
    public static byte[] getMergeBytes(byte[] src, byte[] des){   
        int ac = src.length;   
        int bc = des.length;   
        byte[] b = new byte[ac + bc];   
        for(int i=0;i<ac;i++){   
            b[i] = src[i];   
        }   
        for(int i=0;i<bc;i++){   
            b[ac + i] = des[i];   
        }   
        return b;
    } 
    
    /**  
     * 合并三个byte数组  <br/>
     * @param src 合并前
     * @param cen 合并中
     * @param des 合并后
     * @return 字节数组
     */
    public static byte[] getMergeBytes(byte[] src, byte[] cen, byte[] des){
    	int ac = src.length;
    	int bc = cen.length;
    	int cc = des.length;
    	byte[] b = new byte[ac + bc + cc];   
        for(int i=0;i<ac;i++){   
            b[i] = src[i];   
        }   
        for(int i=0;i<bc;i++){   
            b[ac + i] = cen[i];   
        }
        for(int i=0;i<cc;i++){   
            b[ac + bc + i] = des[i];   
        }
        return b;
    }
    
    /**
     * 5个byte合并<br/>
     * @param a
     * @param b
     * @param c
     * @param d
     * @param e
     * @return
     */
    public static byte[] getMergeBytesFive(byte[] a, byte[] b , byte[] c , byte[] d , byte[] e){
    	int ia = a.length;
    	int ib = b.length;
    	int ic = c.length;
    	int id = d.length;
    	int ie = e.length;
    	byte[] arrs = new byte[ia+ib+ic];
    	arrs = getMergeBytes(a, b, c);
    	byte[] twoArr = new byte[id+ie];
    	twoArr = getMergeBytes(d, e);
    	byte[] bs = new byte[ia+ib+ic+id+ie];
    	bs = getMergeBytes(arrs, twoArr);
    	return bs;
    }
    
}


分享到:
评论

相关推荐

    hex2byte byte2hex

    hex2byte byte2hex,转换成字符串传输

    VB ASP MD5 SHA HMAC AES GZIP BASE64 微信公众号EncodingAESKey 十全大补DLL

    abytData : 字节数组 asp里请加()号 如: bytes="" md5_byte((bytes),0,0) 可选参数 iStart iEnd 默认为0,取整个 bytes的hash值 ---------------------------------------------------- [可选] iStart: 起始位置 ...

    hex2bin_2.2_XiaZaiBa.zip

    -p [value] Pad-byte value in hex (default: ff) -r [start] [end] Range to compute checksum over (default is min and max addresses) -s [address] Starting address in hex for binary file (default: ...

    强大的免费的十六进制编辑器

    XVI32 is a free hex-editor with the following main features: - New: Runs on systems with &gt; 2 GB virtual memory - New: Bugfix (script command CHARCON is now working) - Simplified search for Unicode ...

    Java中byte[]、String、Hex字符串等转换的方法

    主要介绍了Java中byte[]、String、Hex字符串等转换的方法,代码很简单,需要的朋友可以参考下

    Python3编码问题 Unicode utf-8 bytes互转方法

    为什么需要本文,因为在对接某些很老的接口的时候,需要传递过去的是16进制的hex字符串,并且要求对传的字符串做编码,这里就介绍了utf-8 Unicode bytes 等等。 #英文使用utf-8 转换成16进制hex字符串的方法 newstr...

    VBA API 32 Serialport MT-SICS

    ' number of bytes actually written, except when the file is opened with ' FILE_FLAG_OVERLAPPED. If the file handle was created for overlapped ' input and output (I/O), the application must adjust the ...

    hex-magic:用十六进制字符串和字节做魔术的Rust宏

    此板条箱提供了用于处理字节和十六进制值的宏。 hex! hex! 是一个宏,它在编译时将字符串文字( "7D2B" )转换为字节数组( [0x7D, 0x2B] )或匹配模式。 assert_eq!(hex!("01020304"), [1, 2, 3, 4]);... let bytes

    nodejs 十六进制字符串型数据与btye型数据相互转换

    const Bytes2HexString = (b)=&gt; { let hexs = ; for (let i = 0; i &lt; b.length; i++) { let hex = (b[i]).toString(16); if (hex.length === 1) { hexs = '0' + hex; } hexs += hex.toUpperCase();

    对Python3中bytes和HexStr之间的转换详解

    hexstring 如:’1C532145697A8B6F’ str 如:’\x1C\x53\x21\x45\x69\x7A\x8B\x6F’ list 如:[0x1C, 0x53, 0x21, 0x45, 0x69, 0x7A, 0x8B, 0x6F] 各种第三方模块(如pyDes),或者自己写的接口中,可能存在由于...

    二进制转换图片.rar

    二进制转换图片 OutputStream o = response.getOutputStream(); // 将图片转换成字符串 File f = new File("f:\\Vista.png");... String imgStr = byte2hex( bytes ); System.out.println( imgStr); ...

    SimIt-ARM-3.0 ARM指令模拟器

    d [addr] dump 256 bytes from memory g [addr] run until addr s [num] run until pc + num*4 t [num] step num instruction(s) T [num] step num instruction(s) with registers dump (*) r [rn] dump value...

    kgb档案压缩console版+源码

    Such coding is within 1 byte of the Shannon limit, log 1/P(y), so compression depends almost entirely on the goodness of the model, P, i.e. how well it estimates the probability distribution of ...

    au3反编译源码

    Will uses the normal 16-byte start signature to detect the start of a script often this signature was modified or is used for a fake script that is just attached to distract & mislead a decompiler....

    Java SM3&SM4; 脱坑版

    (甚至出现传入 bytes 传出 hex 的情况) 新的模式只需要以下几个函数即可,无需知道内部工作原理: SMUtils.SM3_calcBuf(byte[] buf); SMUtils.SM4_ECB_encodeBytes(byte[] inBuf, byte[] password); SMUtils....

    Free Hex Control

    / Hi every body, thanks for choosing Free Hex Control! / / Errrrr...Actually, I hesitate to release the source code of this control, / Because when I checked after completion, I found that it's ...

    JVIntelHex:一个 javascript Intel HEX 格式编写器(一个非常轻量级但功能强大的实现!)

    合资英特尔HEX 一个 javascript Intel HEX 格式编写器(轻量级实现) 1.0 版由创建 ...// How many bytes per HEX record. Usually 16 or 32 but can take arbitrary numbers. byteCount = 16 ; // Y

    无组件ASP文件上传源代码

    toByte = toByte & chrB("&H"&iLow) & chrB("&H"&iHigh) Else toByte = toByte & chrB(AscB(c)) End If Next End function End Class Class FileInfo dim FormName,FileName,FilePath,FileSize,FileStart ...

    Android开发人员不得不收集的代码

    bytes2HexString, hexString2Bytes : byteArr 与 hexString 互转 chars2Bytes, bytes2Chars : charArr 与 byteArr 互转 memorySize2Byte, byte2MemorySize : 以 unit 为单位的内存大小与字节数互转 byte2...

    asp连接数据库代码实例

    连接数据库代码实例 1,连接数据库代码 文件名称 conn.asp 所有访问数据库的文件都调用此文件&lt;!--#include file=\"Conn.asp\"--&gt; db=\"data/data.mdb\" \'数据库存放目录 on error resume next ...

Global site tag (gtag.js) - Google Analytics