JAVA_BinaryTools_基本类型转换二进制

原创 2016年08月29日 19:46:27
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]);
		}
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

java byte 与 binary 转换

在写通讯程序时,有时需要解析二进制数据流,可能会遇到java数值类型与二进制byte数组的类型转换问题。因此,笔者提供下面代码实例,供大家需要时参考。 import java.io.*; /** *...
  • PZ0605
  • PZ0605
  • 2015-05-10 10:45
  • 1913

Java学习笔记(17) Binary I/O

17.1 Introduction 1、Files can be classified as either text or binary.(文件可以被归类为文本文件和二进制文件) 2、A f...

Java与数据库数据类型对应表

Java中的数据类型和SQL中的数据类型有很多不一样,需要仔细区分,不然易在开发中造成莫名的错误。Java数据类型Hibernate数据类型标准SQL数据类型(PS:对于不同的DB可能有所差异)byt...

java基本类型转换

一. 隐式转换 java中的基本数据类型有8种,他们是boolean,byte,char,short,int,long,float,double.其中boolean比较特殊,他不参与隐式转换。所以我们...

Java基本类型转换的优先顺序

Java中经常碰到两个不同类型的基本类型数据进行运算,当没有强制类型转化时,它们之间总会有一个标准去衡量最终的计算结果是什么类型的吧,下面以一个简单的例子看看默认的优先顺序:public class ...

疯狂Java笔记:3.5 基本类型转换

基本类型转换 1. 自动类型转换 2. 强制类型转换 3. 表达式类型自动提升

Java基本类型转换成字节流

简单的做了点基 /**      *      * double转换byte      *      * @param arr    ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)