byte[]数组与各数据类型的转换大集合

做的程序有时候会需要用到,记录下

 

浮点数Double与byte[]数组互转方法:

	static double ArryToDouble(byte[] Array,int Pos)   
	{ 
		long accum = 0; 
		accum = Array[Pos+0] & 0xFF;
		accum |= (long)(Array[Pos+1] & 0xFF)<<8;
		accum |= (long)(Array[Pos+2] & 0xFF)<<16;
		accum |= (long)(Array[Pos+3] & 0xFF)<<24;
		accum |= (long)(Array[Pos+4] & 0xFF)<<32;
		accum |= (long)(Array[Pos+5] & 0xFF)<<40;
		accum |= (long)(Array[Pos+6] & 0xFF)<<48;
		accum |= (long)(Array[Pos+7] & 0xFF)<<56;
		return Double.longBitsToDouble(accum); 
	}
	
	static byte[] DoubleToArray(double Value)
	{
		long accum = Double.doubleToRawLongBits(Value);
		byte[] byteRet = new byte[8];
		byteRet[0] = (byte)(accum & 0xFF);
		byteRet[1] = (byte)((accum>>8) & 0xFF);
		byteRet[2] = (byte)((accum>>16) & 0xFF);
		byteRet[3] = (byte)((accum>>24) & 0xFF);
		byteRet[4] = (byte)((accum>>32) & 0xFF);
		byteRet[5] = (byte)((accum>>40) & 0xFF);
		byteRet[6] = (byte)((accum>>48) & 0xFF);
		byteRet[7] = (byte)((accum>>56) & 0xFF);
		return byteRet;
	}

	static float ArryToFloat(byte[] Array,int Pos)   
	{ 
		int accum = 0; 
		accum = Array[Pos+0] & 0xFF;
		accum |= (long)(Array[Pos+1] & 0xFF)<<8;
		accum |= (long)(Array[Pos+2] & 0xFF)<<16;
		accum |= (long)(Array[Pos+3] & 0xFF)<<24;
		return Float.intBitsToFloat(accum); 
	}
	
	static byte[] FloatToArray(float Value)
	{
		int accum = Float.floatToRawIntBits(Value);
		byte[] byteRet = new byte[4];
		byteRet[0] = (byte)(accum & 0xFF);
		byteRet[1] = (byte)((accum>>8) & 0xFF);
		byteRet[2] = (byte)((accum>>16) & 0xFF);
		byteRet[3] = (byte)((accum>>24) & 0xFF);
		return byteRet;
	}


 

package common.util;

/**
 * 对数字和字节进行转换。<br>
 * 基础知识:<br>
 * 假设数据存储是以大端模式存储的:<br>
 * byte: 字节类型 占8位二进制 00000000<br>
 * char: 字符类型 占2个字节 16位二进制 byte[0] byte[1]<br>
 * int : 整数类型 占4个字节 32位二进制 byte[0] byte[1] byte[2] byte[3]<br>
 * long: 长整数类型 占8个字节 64位二进制 byte[0] byte[1] byte[2] byte[3] byte[4] byte[5]
 * byte[6] byte[7]<br>
 * float: 浮点数(小数) 占4个字节 32位二进制 byte[0] byte[1] byte[2] byte[3]<br>
 * double: 双精度浮点数(小数) 占8个字节 64位二进制 byte[0] byte[1] byte[2] byte[3] byte[4]
 * byte[5] byte[6] byte[7]<br>
 */
public class NumberBytes {

	/**
	 * 将一个2位字节数组转换为char字符。<br>
	 * 注意,函数中不会对字节数组长度进行判断,请自行保证传入参数的正确性。
	 * 
	 * @param b
	 *            字节数组
	 * @return char字符
	 */
	public static char bytesToChar(byte[] b) {
		char c = (char) ((b[0] << 8) & 0xFF00L);
		c |= (char) (b[1] & 0xFFL);
		return c;
	}

	/**
	 * 将一个8位字节数组转换为双精度浮点数。<br>
	 * 注意,函数中不会对字节数组长度进行判断,请自行保证传入参数的正确性。
	 * 
	 * @param b
	 *            字节数组
	 * @return 双精度浮点数
	 */
	public static double bytesToDouble(byte[] b) {
		return Double.longBitsToDouble(bytesToLong(b));
	}

	/**
	 * 将一个4位字节数组转换为浮点数。<br>
	 * 注意,函数中不会对字节数组长度进行判断,请自行保证传入参数的正确性。
	 * 
	 * @param b
	 *            字节数组
	 * @return 浮点数
	 */
	public static float bytesToFloat(byte[] b) {
		return Float.intBitsToFloat(bytesToInt(b));
	}

	/**
	 * 将一个4位字节数组转换为4整数。<br>
	 * 注意,函数中不会对字节数组长度进行判断,请自行保证传入参数的正确性。
	 * 
	 * @param b
	 *            字节数组
	 * @return 整数
	 */
	public static int bytesToInt(byte[] b) {
		int i = (b[0] << 24) & 0xFF000000;
		i |= (b[1] << 16) & 0xFF0000;
		i |= (b[2] << 8) & 0xFF00;
		i |= b[3] & 0xFF;
		return i;
	}

	/**
	 * 将一个8位字节数组转换为长整数。<br>
	 * 注意,函数中不会对字节数组长度进行判断,请自行保证传入参数的正确性。
	 * 
	 * @param b
	 *            字节数组
	 * @return 长整数
	 */
	public static long bytesToLong(byte[] b) {
		long l = ((long) b[0] << 56) & 0xFF00000000000000L;
		// 如果不强制转换为long,那么默认会当作int,导致最高32位丢失
		l |= ((long) b[1] << 48) & 0xFF000000000000L;
		l |= ((long) b[2] << 40) & 0xFF0000000000L;
		l |= ((long) b[3] << 32) & 0xFF00000000L;
		l |= ((long) b[4] << 24) & 0xFF000000L;
		l |= ((long) b[5] << 16) & 0xFF0000L;
		l |= ((long) b[6] << 8) & 0xFF00L;
		l |= (long) b[7] & 0xFFL;
		return l;
	}

	/**
	 * 将一个char字符转换位字节数组(2个字节),b[0]存储高位字符,大端
	 * 
	 * @param c
	 *            字符(java char 2个字节)
	 * @return 代表字符的字节数组
	 */
	public static byte[] charToBytes(char c) {
		byte[] b = new byte[8];
		b[0] = (byte) (c >>> 8);
		b[1] = (byte) c;
		return b;
	}

	/**
	 * 将一个双精度浮点数转换位字节数组(8个字节),b[0]存储高位字符,大端
	 * 
	 * @param d
	 *            双精度浮点数
	 * @return 代表双精度浮点数的字节数组
	 */
	public static byte[] doubleToBytes(double d) {
		return longToBytes(Double.doubleToLongBits(d));
	}

	/**
	 * 将一个浮点数转换为字节数组(4个字节),b[0]存储高位字符,大端
	 * 
	 * @param f
	 *            浮点数
	 * @return 代表浮点数的字节数组
	 */
	public static byte[] floatToBytes(float f) {
		return intToBytes(Float.floatToIntBits(f));
	}

	/**
	 * 将一个整数转换位字节数组(4个字节),b[0]存储高位字符,大端
	 * 
	 * @param i
	 *            整数
	 * @return 代表整数的字节数组
	 */
	public static byte[] intToBytes(int i) {
		byte[] b = new byte[4];
		b[0] = (byte) (i >>> 24);
		b[1] = (byte) (i >>> 16);
		b[2] = (byte) (i >>> 8);
		b[3] = (byte) i;
		return b;
	}

	/**
	 * 将一个长整数转换位字节数组(8个字节),b[0]存储高位字符,大端
	 * 
	 * @param l
	 *            长整数
	 * @return 代表长整数的字节数组
	 */
	public static byte[] longToBytes(long l) {
		byte[] b = new byte[8];
		b[0] = (byte) (l >>> 56);
		b[1] = (byte) (l >>> 48);
		b[2] = (byte) (l >>> 40);
		b[3] = (byte) (l >>> 32);
		b[4] = (byte) (l >>> 24);
		b[5] = (byte) (l >>> 16);
		b[6] = (byte) (l >>> 8);
		b[7] = (byte) (l);
		return b;
	}

}

 


 

 

string和int之间的转换?

字符串转换成数据

String MyNumber ="1234";   
int MyInt = Integer.parseInt(MyNumber);  


 

字符串转换成byte, short, int, float, double, long等数据类型,可以分别参考Byte, Short, Integer, Float, Double, Long类的parseXXX 方法。

01.a1=Integer.parseInt(s1);   
02.s1=Integer.toString(a1);  


 

数据转换成字符串

int MyInt = 1234;   

String MyString = "" + MyInt;


Java中byte数组与long数组相互转化
ByteAndLongArrayTest代码如下:

01.package com.array.test;
02.03.import java.io.IOException;
04.05./**
06. * @ version 1.0
07. * @ author  wangwei
08. * @ created on  2008-10-17
09. */10.public class ByteAndLongArrayTest {
11.12.    /**
13.     * long数组转化为byte数组
14.     * 
15.     * @param longArray
16.     * @return
17.     * @throws IOException
18.     */19.    public static byte[] longToByte(long[] longArray)throws IOException{
20.        byte[] byteArray=new byte[longArray.length*8];
21.        for(int i=0;i<longArray.length;i++)
22.        {
23.            byteArray[0+8*i]=(byte)(longArray[i]>>56);
24.            byteArray[1+8*i]=(byte)(longArray[i]>>48);
25.            byteArray[2+8*i]=(byte)(longArray[i]>>40);
26.            byteArray[3+8*i]=(byte)(longArray[i]>>32);
27.            byteArray[4+8*i]=(byte)(longArray[i]>>24);
28.            byteArray[5+8*i]=(byte)(longArray[i]>>16);
29.            byteArray[6+8*i]=(byte)(longArray[i]>>8);
30.            byteArray[7+8*i]=(byte)(longArray[i]>>0);
31.        }
32.        return byteArray;
33.    }
34.    
35.    /**
36.     * byte数组转化为long数组
37.     * 
38.     * @param byteArray
39.     * @return
40.     * @throws IOException
41.     */42.    public static long[] byteToLong(byte[] byteArray)throws IOException{
43.        
44.        long [] longArray=new long[byteArray.length/8];
45.        for(int i=0;i<longArray.length;i++)
46.        {
47.            longArray[i]=(((long)byteArray[0+8*i]&0xff)<<56)
48.                        |(((long)byteArray[1+8*i]&0xff)<<48)
49.                        |(((long)byteArray[2+8*i]&0xff)<<40)
50.                        |(((long)byteArray[3+8*i]&0xff)<<32)
51.                        |(((long)byteArray[4+8*i]&0xff)<<24)
52.                        |(((long)byteArray[5+8*i]&0xff)<<16)
53.                        |(((long)byteArray[6+8*i]&0xff)<<8)
54.                        |(((long)byteArray[7+8*i]&0xff)<<0);
55.            
56.        }
57.        return longArray;
58.    }
59.60.    /**
61.     * @param args
62.     */63.    public static void main(String[] args) throws Exception{
64.        // TODO Auto-generated method stub65.        long [] longArray={1,2,3,4,500000};
66.        byte [] byteArray=longToByte(longArray);
67.        for(int i=0;i<byteArray.length;i++)
68.        {
69.            System.out.print(byteArray[i]+" ");
70.        }
71.        System.out.println();
72.        long [] longArray2=byteToLong(byteArray);
73.        for(int i=0;i<longArray.length;i++)
74.            System.out.print(longArray[i]+" ");
75.    }
76.}


 

 

 

其它数据类型可以利用同样的方法转换成字符串。

十进制到其他进制的转换
十进制整数转换成二进制整数,返回结果是一个字符串:
Integer.toBinaryString(int i);
Integer和Long提供了toBinaryString, toHexString和toOctalString方法,可以方便的将数据转换成二进制、十六进制和八进制字符串。功能更加强大的是其toString(int/long i, int radix)方法,可以将一个十进制数转换成任意进制的字符串形式。
byte, short, float和double等数据类型,可以利用Integer或者是Long的toBinaryString, toHexString, to OctalString和toString方法转换成其他进制的字符串形式。

其它进制到十进制的转换
五进制字符串14414转换成十进制整数,结果是1234:
System.out.println(Integer.valueOf("14414", 5);
Integer和Long提供的valueOf(String source, int radix)方法,可以
将任意进制的字符串转换成十进制数据。

把String类型转换成16进制的整数

public static void main(String args[]){  
String x = "0x300C8";  
int y = Integer.decode(x).intvalue();  
System.out.println(y);  
}  
int、char、double与byte相互转换的程序
整数到字节数组的转换
<pre class="java" name="code">public static byte[] intToByte(int number) {  
  int temp = number;  
  byte[] b=new byte[4];  
  for (int i=b.length-1;i>-1;i--){  
    b[i] = new Integer(temp&0xff).byteValue();      //将最高位保存在最低位  
    temp = temp >> 8;       //向右移8位  
  }  
  return b;  
}  
 
 
字节数组到整数的转换
  
 
 
Java代码   收藏代码
  1. public static int byteToInt(byte[] b) {  
  2.     int s = 0;  
  3.     for (int i = 0; i < 3; i++) {  
  4.       if (b[i] >= 0)  
  5.         s = s + b[i];  
  6.       else  
  7.         s = s + 256 + b[i];  
  8.       s = s * 256;  
  9.     }  
  10.     if (b[3] >= 0)       //最后一个之所以不乘,是因为可能会溢出  
  11.       s = s + b[3];  
  12.     else  
  13.       s = s + 256 + b[3];  
  14.     return s;  
  15.   }  


短整数与字节数组之间的相互转换 
short与int之间的区别在于short是两个字节的,而int是四个字节的。因此,只需要将5 与6 中的范例程序小做改动,即可实现短整数与字节数组之间的相互转换。 


  字符到字节转换
  
 
 
Java代码   收藏代码
  1. public static byte[] charToByte(char ch){  
  2.     int temp=(int)ch;  
  3.     byte[] b=new byte[2];  
  4.     for (int i=b.length-1;i>-1;i--){  
  5.       b[i] = new Integer(temp&0xff).bytevalue();      //将最高位保存在最低位  
  6.       temp = temp >> 8;       //向右移8位  
  7.     }  
  8.     return b;  
  9.   }  


  //字节到字符转换
  
 
 
Java代码   收藏代码
  1. public static char byteToChar(byte[] b){  
  2.     int s=0;  
  3.     if(b[0]>0)  
  4.       s+=b[0];  
  5.     else  
  6.       s+=256+b[0];  
  7.     s*=256;  
  8.     if(b[1]>0)  
  9.       s+=b[1];  
  10.     else  
  11.       s+=256+b[1];  
  12.     char ch=(char)s;  
  13.     return ch;  
  14.   }  


  浮点到字节转换
  
 
 
Java代码   收藏代码
  1. public static byte[] doubleToByte(double d){  
  2.     byte[] b=new byte[8];  
  3.     long l=Double.doubleToLongBits(d);  
  4.     for(int i=0;i<b.length;i++){  
  5.       b[i]=new Long(l).bytevalue();  
  6.       l=l>>8;  
  7.     }  
  8.     return b;  
  9.   }  


  字节到浮点转换
  
 
 
Java代码   收藏代码
  1. public static double byteToDouble(byte[] b){  
  2.     long l;  
  3.     l=b[0];  
  4.     l&=0xff;  
  5.     l|=((long)b[1]<<8);  
  6.     l&=0xffff;  
  7.     l|=((long)b[2]<<16);  
  8.     l&=0xffffff;  
  9.     l|=((long)b[3]<<24);  
  10.     l&=0xffffffffl;  
  11.     l|=((long)b[4]<<32);  
  12.     l&=0xffffffffffl;  
  13.    
  14.     l|=((long)b[5]<<40);  
  15.     l&=0xffffffffffffl;  
  16.     l|=((long)b[6]<<48);  
  17.   
  18.   
  19.     l|=((long)b[7]<<56);  
  20.     return Double.longBitsToDouble(l);  
  21.   }  


int与byte array之间的转换程序 
在通讯中经常需要将数值转换成字节流,或者是将字节流转换成数值。下面
提供的程序可以进行int和byte array之间的转换。

 
 
Java代码   收藏代码
  1. /** 
  2.  * 
  3.  * IntConverter 
  4.  * 
  5.  * This class provides methods to convert int into byte array and 
  6.  * byte array back into int. 
  7.  * 
  8.  */  
  9. public class IntConverter  
  10. {  
  11.         /** 
  12.          * 
  13.          * Method converting int into byte array. 
  14.          * 
  15.          * @param number The int value to be converted. 
  16.          * 
  17.          */  
  18.         public static byte[] toByteArray(int number)  
  19.         {  
  20.             int temp = number;  
  21.             byte[] b=new byte[4];  
  22.             for (int i = b.length - 1; i > -1; i--)  
  23.             {  
  24.                 b[i] = new Integer(temp & 0xff).bytevalue();  
  25.                 temp = temp >> 8;  
  26.             }  
  27.             return b;  
  28.         }  
  29.    
  30.         /** 
  31.          * 
  32.          * Method converting byte array into int. 
  33.          * 
  34.          * @param The byte array to be converted. 
  35.          * 
  36.          */  
  37.         public static int toInteger(byte[] b)  
  38.         {  
  39.             int s = 0;  
  40.             for (int i = 0; i < 3; i++)  
  41.             {  
  42.                 if (b[i] > 0)  
  43.                     s = s + b[i];  
  44.                 else  
  45.                     s = s + 256 + b[i];  
  46.                 s = s * 256;  
  47.             }  
  48.             if (b[3] > 0)  
  49.                 s = s + b[3];  
  50.             else  
  51.                 s = s + 256 + b[3];  
  52.             return s;  
  53.         }  
  54.    
  55.     // Testing program.  
  56.     public static void main(String[] args)  
  57.     {  
  58.         IntConverter abc = new IntConverter();  
  59.         int s = -1121115678;  
  60.         byte[] b = abc.toByteArray(s);  
  61.         for (int i = 0; i <= 3; i++)  
  62.                 System.out.println(b[i]);  
  63.         s = abc.toInteger(b);  
  64.         System.out.println(s);  
  65.     }  
  66. }  


字节数组到整数的转换

 
 
Java代码   收藏代码
  1. public static int byteToint(byte[] convertByteValue)  
  2.      { byte[] YY=new byte[4];  
  3.       YY=convertByteValue;  
  4.       int ee, ff, gg, hh;  
  5.       ee = YY[3] & 0x000000ff;  
  6.       //System.out.println("ee: " +ee);  
  7.       ff = (YY[2]<<8) & 0x0000ff00;  
  8.       //System.out.println("ff: " +ff);  
  9.       gg = (YY[1]<<16) & 0x00ff0000;  
  10.       // System.out.println("gg: " +gg);  
  11.       hh = YY[0]<<24;  
  12.       // System.out.println("hh: "+hh);  
  13.       int jj = ee + ff + gg + hh;  
  14.       // System.out.println("jj: "+jj);  
  15.       return jj;  
  16.       }  


    整数到字节数组的转换

   
 
 
Java代码   收藏代码
  1. public byte[] intTobyte(int convertIntValue)  
  2.     { int Y;  
  3.       Y = 1321432453;  
  4.       byte YY[] = new byte[4];  
  5.       Integer aa = new Integer(Y);  
  6.        YY[3] = aa.byteValue();  
  7.       Integer bb = new Integer(Y>>>8);  
  8.        YY[2] = bb.byteValue();  
  9.       Integer cc = new Integer(Y>>>16);  
  10.        YY[1] = cc.byteValue();  
  11.       Integer dd = new Integer(Y>>>24);  
  12.        YY[0] = dd.byteValue();  
  13.        return YY;  
  14.        } 

 
 
如何将字串 String 转换成整数 int?
  A. 有两个方法:
  1). int i = Integer.parseInt([String]); 或
  i = Integer.parseInt([String],[int radix]);
  2). int i = Integer.valueOf(my_str).intValue();
  注: 字串转成 Double, Float, Long 的方法大同小异.
  2 如何将整数 int 转换成字串 String ?
  A. 有叁种方法:
  1.) String s = String.valueOf(i);
  2.) String s = Integer.toString(i);
  3.) String s = "" + i;
  注: Double, Float, Long 转成字串的方法大同小异.
  这是一个例子,说的是JAVA中数据数型的转换.供大家学习引
  package cn.com.lwkj.erts.reGISter;
  import java.sql.Date;
  public class TypeChange {
  public TypeChange() {
  }
  //change the string type to the int type
  public static int stringToInt(String intstr)
  {
  Integer integer;
  integer = Integer.valueOf(intstr);
  return integer.intValue();
  }
  //change int type to the string type
  public static String intToString(int value)
  {
  Integer integer = new Integer(value);
  return integer.toString();
  }
  //change the string type to the float type
  public static float stringToFloat(String floatstr)
  {
  Float floatee;
  floatee = Float.valueOf(floatstr);
  return floatee.floatValue();
  }
  //change the float type to the string type
  public static String floatToString(float value)
  {
  Float floatee = new Float(value);
  return floatee.toString();
  }
  //change the string type to the sqlDate type
  public static java.sql.Date stringToDate(String dateStr)
  {
  return java.sql.Date.valueOf(dateStr);
  }
  //change the sqlDate type to the string type
  public static String dateToString(java.sql.Date datee)
  {
  return datee.toString();
  }
  public static void main(String[] args)
  {
  java.sql.Date day ;
  day = TypeChange.stringToDate("2003-11-3");
  String strday = TypeChange.dateToString(day);
  System.out.println(strday);
  }
  }
  JAVA中常用数据类型转换函数
  虽然都能在JAVA API中找到,整理一下做个备份。
  string->byte
  Byte static byte parseByte(String s)
  byte->string
  Byte static String toString(byte b)
  char->string
  Character static String to String (char c)
  string->Short
  Short static Short parseShort(String s)
  Short->String
  Short static String toString(Short s)
  String->Integer
  Integer static int parseInt(String s)
  Integer->String
  Integer static String tostring(int i)
  String->Long
  Long static long parseLong(String s)
  Long->String
  Long static String toString(Long i)
  String->Float
  Float static float parseFloat(String s)
  Float->String
  Float static String toString(float f)
  String->Double
  Double static double parseDouble(String s)
  Double->String
  Double static String toString(Double d)
 
public class T3 {

	public static void main(String[] args){
		String filePath = "E:\\softoon\\workspace_softoon\\TestMobile\\src\\1.docx";
		String outFilePath = "E:\\softoon\\workspace_softoon\\TestMobile\\src";
		String outFileName = "2.docx";
		
		getFile(getBytes(filePath),outFilePath,outFileName);
	}

	/**
	 * 获得指定文件的byte数组
	 */
	public static byte[] getBytes(String filePath){
		byte[] buffer = null;
		try {
			File file = new File(filePath);
			FileInputStream fis = new FileInputStream(file);
			ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
			byte[] b = new byte[1000];
			int n;
			while ((n = fis.read(b)) != -1) {
				bos.write(b, 0, n);
			}
			fis.close();
			bos.close();
			buffer = bos.toByteArray();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buffer;
	}

	/**
	 * 根据byte数组,生成文件
	 */
	public static void getFile(byte[] bfile, String filePath,String fileName) {
		BufferedOutputStream bos = null;
		FileOutputStream fos = null;
		File file = null;
		try {
			File dir = new File(filePath);
			if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
				dir.mkdirs();
			}
			file = new File(filePath+"\\"+fileName);
			fos = new FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			bos.write(bfile);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
	}
}
 
 
 
<pre style="PADDING-BOTTOM: 0px; MARGIN: 10px 0px 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; WORD-WRAP: break-word; PADDING-TOP: 0px"><pre class="java" name="code">public class NumberUtil {
	/**
	 * int整数转换为4字节的byte数组
	 * 
	 * @param i
	 *            整数
	 * @return byte数组
	 */
	public static byte[] intToByte4(int i) {
		byte[] targets = new byte[4];
		targets[3] = (byte) (i & 0xFF);
		targets[2] = (byte) (i >> 8 & 0xFF);
		targets[1] = (byte) (i >> 16 & 0xFF);
		targets[0] = (byte) (i >> 24 & 0xFF);
		return targets;
	}

	/**
	 * long整数转换为8字节的byte数组
	 * 
	 * @param lo
	 *            long整数
	 * @return byte数组
	 */
	public static byte[] longToByte8(long lo) {
		byte[] targets = new byte[8];
		for (int i = 0; i < 8; i++) {
			int offset = (targets.length - 1 - i) * 8;
			targets[i] = (byte) ((lo >>> offset) & 0xFF);
		}
		return targets;
	}

	/**
	 * short整数转换为2字节的byte数组
	 * 
	 * @param s
	 *            short整数
	 * @return byte数组
	 */
	public static byte[] unsignedShortToByte2(int s) {
		byte[] targets = new byte[2];
		targets[0] = (byte) (s >> 8 & 0xFF);
		targets[1] = (byte) (s & 0xFF);
		return targets;
	}

	/**
	 * byte数组转换为无符号short整数
	 * 
	 * @param bytes
	 *            byte数组
	 * @return short整数
	 */
	public static int byte2ToUnsignedShort(byte[] bytes) {
		return byte2ToUnsignedShort(bytes, 0);
	}

	/**
	 * byte数组转换为无符号short整数
	 * 
	 * @param bytes
	 *            byte数组
	 * @param off
	 *            开始位置
	 * @return short整数
	 */
	public static int byte2ToUnsignedShort(byte[] bytes, int off) {
		int high = bytes[off];
		int low = bytes[off + 1];
		return (high << 8 & 0xFF00) | (low & 0xFF);
	}

	/**
	 * byte数组转换为int整数
	 * 
	 * @param bytes
	 *            byte数组
	 * @param off
	 *            开始位置
	 * @return int整数
	 */
	public static int byte4ToInt(byte[] bytes, int off) {
		int b0 = bytes[off] & 0xFF;
		int b1 = bytes[off + 1] & 0xFF;
		int b2 = bytes[off + 2] & 0xFF;
		int b3 = bytes[off + 3] & 0xFF;
		return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
	}
}


 


                
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值