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
  • 2277

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

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

Java的binarySearch

public static int binarySearch(Object[] a, Object key) ...{    int low = 0;    int high = a.length-1...

java8中几个函数式接口的小例子

// Function -T作为输入,返回的R作为输出 Function function = (x) -> {System.out.print(x+": ");return "Function...

Java读写二进制文件

  import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io....

【LeetCode-面试算法经典-Java实现】【067-Add Binary(二进制加法)】

【067-Add Binary(二进制加法)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given two binary strings, return their ...

java binary name

一次碰到java binary name,搜了好久都没有找到是什么意思。然后研究了一会才知道原来就是java类的原名呀比如Object的binary name是java.lang.ObjectThre...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Java和mysql的数据类型对应

类型名称 显示长度 数据库类型 JAVA类型 JDBC类型索引(int) 描述 VARCHAR L+N VARCHAR java.lang.String 12 CHAR N CHAR j...
  • woshisap
  • woshisap
  • 2011年08月30日 13:26
  • 32098

数据库类型与java数据类型的对应关系

以下转自:http://kummy.itpub.net/post/17165/172850 本文在原文基础上有增减。 本概述是从《JDBCTM Database Access from Ja...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:JAVA_BinaryTools_基本类型转换二进制
举报原因:
原因补充:

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