您的位置:首页 > 编程语言 > Java开发

Java中字节数组与基本数据类型的转换

2013-12-20 11:56 671 查看
ByteTools工具类,将基本数据类型转换成字节数组

public class ByteTools {
/**
* Convert short to byte array.
* @param number
* @return
*/
public static byte[] shortToByteArray(short number) {
int temp = number;
byte[] b = new byte[2];
for (int i = 0; i < b.length; i++) {
b[i] = Integer.valueOf(temp & 0xff).byteValue();
temp = temp >> 8;
}
return b;
}

/**
* int类型转换成ByteArray
*
* @param integer
* @return
*/
public static byte[] intToByteArray(int integer) {

int byteNum = (40 - Integer.numberOfLeadingZeros(integer < 0 ? ~integer
: integer)) / 8;
byte[] byteArray = new byte[4];

for (int n = 0; n < byteNum; n++)
byteArray[3 - n] = (byte) (integer >>> (n * 8));

return (byteArray);
}

/**
* float类型转换成ByteArray
*
* @param ft
* @return
*/
public static byte[] floatToByteArray(float ft) {
ByteArrayOutputStream boutput = new ByteArrayOutputStream();
DataOutputStream doutput = new DataOutputStream(boutput);
try {
doutput.writeFloat(ft);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] buf = boutput.toByteArray();
return buf;
}

/**
* double类型转换成ByteArray
*
* @param val
* @return
*/
public static byte[] doubleToByteArray(double val) {
ByteArrayOutputStream boutput = new ByteArrayOutputStream();
DataOutputStream doutput = new DataOutputStream(boutput);
try {
doutput.writeDouble(val);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] buf = boutput.toByteArray();
return buf;
}

/**
* String类型转换成ByteArray
*
* @param str
* @return
*/
public static byte[] stringToByteArray(String str) {
return str.getBytes();
}

/**
* ByteArray类型的累加
*
* @param byte1
* @param byte2
* @return
*/
public static byte[] addByteArray(byte[] byte1, byte[] byte2) {
byte[] data = new byte[byte1.length + byte2.length];
System.arraycopy(byte1, 0, data, 0, byte1.length);
System.arraycopy(byte2, 0, data, byte1.length, byte2.length);
return data;
}

/**
* ByteArray类型的累加
*
* @param byte1
* @param byte2
* @return
*/
public static byte[] addByteArray(byte[] byte1, byte[] byte2, byte[] byte3) {
byte[] data = addByteArray(addByteArray(byte1, byte2), byte3);
return data;
}
/**
* 四个byte累加
* @param byte1
* @param byte2
* @param byte3
* @param byte4
* @return
*/
public static byte[] addByteArray(byte[] byte1, byte[] byte2,byte[] byte3, byte[] byte4) {
byte[] data1 = addByteArray(byte1,byte2);
byte[] data2 = addByteArray(byte3,byte4);
return addByteArray(data1,data2);
}

}


ByteParseTools工具类,将字节数组转换成基本数据类型

public class ByteParseTools {
/**
* 截取byte数据
*
* @param b
*            是byte数组
* @param j
*            是大小
* @param k
*            是从哪里开始
* @return
*/
public static byte[] cutOutByte(byte[] b, int j, int k) {
if (b.length == 0 || j == 0) {
return null;
}
byte[] bjq = new byte[j];
for (int i = 0; i < j; i++) {
bjq[i] = b[i + k];
}
return bjq;
}
/**
* 将字节数组转成int类型
*
* @param b
* @return
*/
public static int byteToInt(byte[] b) {

int mask = 0xff;
int temp = 0;
int n = 0;
for (int i = 0; i < 4; i++) {
n <<= 8;
temp = b[i] & mask;
n |= temp;
}
return n;
}
/**
* Convert byte array to short.
*
* @param b
* @return
*/
public static short byteToShort(byte[] b) {
short s = 0;
short s0 = (short) (b[0] & 0xff);
short s1 = (short) (b[1] & 0xff);
s1 <<= 8;
s = (short) (s0 | s1);
return s;
}
/**
* 将byte转成String
*
* @param data
* @return
*/
public static String byteToString(byte[] data) {
return new String(data);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: