关闭

JAVA_BinaryTools_基本类型转换二进制

标签: java工具
372人阅读 评论(0) 收藏 举报
分类:
import java.io.UnsupportedEncodingException;
import java.util.List;


public class BinaryTools{
	
	public static final int BOOLEAN_BYTES=1;
	public static final int CHAR_BYTES=Character.SIZE/8;
	public static final int SHORT_BYTES=Short.SIZE/8;
	public static final int INT_BYTES=Integer.SIZE/8;
	public static final int LONG_BYTES=Long.SIZE/8;
	public static final int DOUBLE_BYTES=Double.SIZE/8;
	
	public static final String CHARSET="UTF-8";
	
	public static final String TAG="BinaryTools";
	
	/**boolean syte**/
	public static byte[] bool2bytes(boolean flag){
		byte[] data=new byte[]{0};
		if(flag) data[0]=(byte) 0xff;
		return data;
	} 
	
	public static byte bool2byte(boolean flag){
		byte data=0;
		if(flag) data=(byte) 0xff;
		return data;
	}
	public static boolean bytes2bool(byte data){
		if(data==0)
			return false;
		return true;
	}
	public static boolean bytes2bool(byte[] data,int offset){
		if(data[offset]==0)	return false;
		return true;
	}
	/**short style**/
	public static byte[] short2bytes(short num){
		byte[] data=new byte[Short.SIZE/8];
		for(int i=0;i<data.length;++i){
			data[i]=(byte)((num>>i*8)&0xff);
		}
		return data;
	}
	public static short bytes2Short(byte[] bRefArr){
		short iOutcome = 0;
	    byte bLoop;
	    for (int i = 0; i < bRefArr.length && i<Short.SIZE/8; i++) {
	        bLoop = bRefArr[i];
	        iOutcome += (bLoop & 0xFF) << (8 * i);
	    }
	    return iOutcome;
	}
	public static short bytes2Short(byte[] bRefArr,int offset){
		short iOutcome = 0;
	    byte bLoop;
	    int i=offset;
	    for (int shiftBy = 0; i < bRefArr.length && shiftBy<Short.SIZE/8; ++shiftBy) {
	        bLoop = bRefArr[i++];
	        iOutcome += (bLoop & 0xFF) << (8 * shiftBy);
	    }
	    return iOutcome;
	}
	public static short bytes2Short(byte[] bRefArr,int offset,int length){
		short iOutcome = 0;
	    byte bLoop;
	    int i=offset;
	    for (int shiftBy = 0; i < bRefArr.length && shiftBy<length && shiftBy<Short.SIZE/8; ++shiftBy) {
	        bLoop = bRefArr[i++];
	        iOutcome += (bLoop & 0xFF) << (8 * shiftBy);
	    }
	    return iOutcome;
	}
	/**int style**/
	public static byte[] int2Bytes(int num){
		byte[] data=new byte[Integer.SIZE/8];
		for(int i=0;i<data.length;++i){
			data[i]=(byte) ((num>>i*8)&0xff);
		}
		return data;
	}
	public static int bytes2Int(byte[] bRefArr) {
	    int iOutcome = 0;
	    byte bLoop;
	    for (int i = 0; i < bRefArr.length && i<Integer.SIZE/8; i++) {
	        bLoop = bRefArr[i];
	        iOutcome += (bLoop & 0xFF) << (8 * i);
	    }
	    return iOutcome;
	}
	public static int bytes2Int(byte[] bRefArr,int offset) {
	    int iOutcome = 0;
	    byte bLoop;
	    int i=offset;
	    for (int shiftBy=0; i<bRefArr.length && shiftBy<Integer.SIZE/8; ++shiftBy) {
	        bLoop = bRefArr[i++];
	        iOutcome += (bLoop & 0xFF) << (8 * shiftBy);
	    }
	    return iOutcome;
	}
	public static int bytes2Int(byte[] bRefArr,int offset,int length) {
	    int iOutcome = 0;
	    byte bLoop;
	    int i=offset;
	    for (int shiftBy=0; shiftBy < length && i<bRefArr.length && shiftBy<Integer.SIZE/8; ++shiftBy) {
	        bLoop = bRefArr[i++];
	        iOutcome += (bLoop & 0xFF) << (8 * shiftBy);
	    }
	    return iOutcome;
	}
	/**long style**/
	public static byte[] long2Bytes(long num){
		byte[] data=new byte[Long.SIZE/8];
		for(int i=0;i<data.length;++i){
			data[i]=(byte) ((num>>i*8)&0xff);
		}
		return data;
	}
	public static long bytes2Long(byte[] bRefArr) {
	    long iOutcome = 0;
	    byte bLoop;
	    for (int i = 0; i < bRefArr.length && i<Long.SIZE/8; i++) {
	        bLoop = bRefArr[i];
	        iOutcome += (bLoop & 0xFF) << (8 * i);
	    }
	    return iOutcome;
	}
	public static long bytes2Long(byte[] bRefArr,int offset) {
	    long iOutcome = 0;
	    byte bLoop;
	    int i=offset;
	    for (int shiftBy=0; i<bRefArr.length && shiftBy<Long.SIZE/8; ++shiftBy) {
	        bLoop = bRefArr[i++];
	        iOutcome += (bLoop & 0xFF) << (8 * shiftBy);
	    }
	    return iOutcome;
	}
	public static long bytes2Long(byte[] bRefArr,int offset,int length) {
	    long iOutcome = 0;
	    byte bLoop;
	    int i=offset;
	    for (int shiftBy=0; shiftBy < length && i<bRefArr.length && shiftBy<Long.SIZE/8; ++shiftBy) {
	        bLoop = bRefArr[i++];
	        iOutcome += (bLoop & 0xFF) << (8 * shiftBy);
	    }
	    return iOutcome;
	}
	/**double style**/
	public static byte[] double2bytes(double num){
		long a=Double.doubleToLongBits(num);
		byte[] data=new byte[Long.SIZE/8];
		for(int i=0;i<data.length;++i){
			data[i]=(byte)((a>>i*8)&0xff);
		}
		return data;
	}
	public static double bytes2double (byte[] arr) {
		long accum = 0;
		for (int i = 0; i < Double.SIZE/8 && i<arr.length; ++i){
			accum |= ((long) (arr[i] & 0xff)) << (i*8);
		}
		return Double.longBitsToDouble(accum);
	}
	public static double bytes2double (byte[] arr,int offset) {
		long accum = 0;
		int i=offset;
		for (int shiftBy = 0; shiftBy < Double.SIZE/8 && i<arr.length; ++shiftBy){
			accum |= ((long) (arr[i++] & 0xff)) << (shiftBy*8);
		}
		return Double.longBitsToDouble(accum);
	}
	public static double bytes2double (byte[] arr,int offset,int length) {
		long accum = 0;
		int i=offset;
		for (int shiftBy = 0; shiftBy < Double.SIZE/8 && shiftBy<length && i<arr.length; ++shiftBy){
			accum |= ((long) (arr[i++] & 0xff)) << (shiftBy*8);
		}
		return Double.longBitsToDouble(accum);
	}
	/**char style**/
	public static byte[] char2bytes(char word){
		byte[] data=new byte[Character.SIZE/8];
		for(int i=0;i<data.length;++i){
			data[i]=(byte)((word>>(i*8))&0xff);
		}
		return data;
	}
	public static char bytes2char(byte[] arr){
		char word = 0;
		for(int i=0;i<arr.length && i<Character.SIZE/8;++i){
			word|=((char)(arr[i]&0xff))<<(i*8);
		}
		return word;
	}
	public static char bytes2char(byte[] arr,int offset){
		char word = 0;
		int i=offset;
		for(int shiftBy=0;i<arr.length && shiftBy<Character.SIZE/8;++shiftBy){
			word|=((char)(arr[i++]&0xff))<<(shiftBy*8);
		}
		return word;
	}
	public static char bytes2char(byte[] arr,int offset,int length){
		char word = 0;
		int i=offset;
		for(int shiftBy=0;i<arr.length && shiftBy<Character.SIZE/8 && shiftBy<length;++shiftBy){
			word|=((char)(arr[i++]&0xff))<<(shiftBy*8);
		}
		return word;
	}
	/**string style**/
	public static byte[] string2bytes(String str){
		return str.getBytes();
	}
	public static byte[] string2bytes(String str,String charset){
		try{
			return str.getBytes(charset);
		}catch (UnsupportedEncodingException e){
			System.out.println( TAG+":nsupportedEncoding");
			return null;
		}
	}
	public static String bytes2String(byte[] arr){
		return new String(arr);
	}
	public static String bytes2String(byte[] arr,int offset,int length){
		return new String(arr,offset,length);
	}
	public static String bytes2String(byte[] arr,int offset,int length,String charset){
		try{
			return new String(arr,offset,length,charset);
		}catch (UnsupportedEncodingException e){
			System.out.println( TAG+":nsupportedEncoding");
			return null;
		}
	}
	/**将arr2插入arr的从offset开始的位置中,并且返回插入后位置**/
	public static int setBytes(byte[] arr,int offset,byte[] arr2){
		if(arr.length-offset+1<arr2.length || offset<0) return -1;
		for(int i=0;i<arr2.length;++i){
			arr[offset++]=arr2[i];
		}
		return offset;
	}
	public static int setBytes(byte[] arr,int offset,byte arr2){
		if(arr.length-offset+1<1 || offset<0) return -1;
		arr[offset++]=arr2;
		return offset;
	}
	public static void setBytes(List<Byte> arr,byte[] arr2){
		if(null==arr) return;
		for(int i=0;i<arr2.length;i++){
			arr.add(arr2[i]);
		}
	}
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:442次
    • 积分:23
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档