Java中byte、byte数组与int、long的转换详解
一、Java中byte和int之间的转换源码:
//byte与int的相互转换
publicstaticbyteintToByte(intx){
return(byte)x;
}
publicstaticintbyteToInt(byteb){
//Java总是把byte当做有符处理;我们可以通过将其和0xFF进行二进制与得到它的无符值
returnb&0xFF;
}
测试代码:
//测试int转byte
intint0=234;
bytebyte0=intToByte(int0);
System.out.println("byte0="+byte0);//byte0=-22
//测试byte转int
intint1=byteToInt(byte0);
System.out.println("int1="+int1);//int1=234
二、Java中byte数组和int之间的转换源码:
//byte数组与int的相互转换
publicstaticintbyteArrayToInt(byte[]b){
returnb[3]&0xFF|
(b[2]&0xFF)<<8|
(b[1]&0xFF)<<16|
(b[0]&0xFF)<<24;
}
publicstaticbyte[]intToByteArray(inta){
returnnewbyte[]{
(byte)((a>>24)&0xFF),
(byte)((a>>16)&0xFF),
(byte)((a>>8)&0xFF),
(byte)(a&0xFF)
};
}
测试代码:
//测试int转byte数组
intint2=1417;
byte[]bytesInt=intToByteArray(int2);
System.out.println("bytesInt="+bytesInt);//bytesInt=[B@de6ced
//测试byte数组转int
intint3=byteArrayToInt(bytesInt);
System.out.println("int3="+int3);//int3=1417
三、Java中byte数组和long之间的转换源码:
privatestaticByteBufferbuffer=ByteBuffer.allocate(8);
//byte数组与long的相互转换
publicstaticbyte[]longToBytes(longx){
buffer.putLong(0,x);
returnbuffer.array();
}
publicstaticlongbytesToLong(byte[]bytes){
buffer.put(bytes,0,bytes.length);
buffer.flip();//needflip
returnbuffer.getLong();
}
测试代码:
//测试long转byte数组
longlong1=2223;
byte[]bytesLong=longToBytes(long1);
System.out.println("bytes="+bytesLong);//bytes=[B@c17164
//测试byte数组转long
longlong2=bytesToLong(bytesLong);
System.out.println("long2="+long2);//long2=2223
四、整体工具类源码:
importjava.nio.ByteBuffer;
publicclassTest{
privatestaticByteBufferbuffer=ByteBuffer.allocate(8);
publicstaticvoidmain(String[]args){
//测试int转byte
intint0=234;
bytebyte0=intToByte(int0);
System.out.println("byte0="+byte0);//byte0=-22
//测试byte转int
intint1=byteToInt(byte0);
System.out.println("int1="+int1);//int1=234
//测试int转byte数组
intint2=1417;
byte[]bytesInt=intToByteArray(int2);
System.out.println("bytesInt="+bytesInt);//bytesInt=[B@de6ced
//测试byte数组转int
intint3=byteArrayToInt(bytesInt);
System.out.println("int3="+int3);//int3=1417
//测试long转byte数组
longlong1=2223;
byte[]bytesLong=longToBytes(long1);
System.out.println("bytes="+bytesLong);//bytes=[B@c17164
//测试byte数组转long
longlong2=bytesToLong(bytesLong);
System.out.println("long2="+long2);//long2=2223
}
//byte与int的相互转换
publicstaticbyteintToByte(intx){
return(byte)x;
}
publicstaticintbyteToInt(byteb){
//Java总是把byte当做有符处理;我们可以通过将其和0xFF进行二进制与得到它的无符值
returnb&0xFF;
}
//byte数组与int的相互转换
publicstaticintbyteArrayToInt(byte[]b){
returnb[3]&0xFF|
(b[2]&0xFF)<<8|
(b[1]&0xFF)<<16|
(b[0]&0xFF)<<24;
}
publicstaticbyte[]intToByteArray(inta){
returnnewbyte[]{
(byte)((a>>24)&0xFF),
(byte)((a>>16)&0xFF),
(byte)((a>>8)&0xFF),
(byte)(a&0xFF)
};
}
//byte数组与long的相互转换
publicstaticbyte[]longToBytes(longx){
buffer.putLong(0,x);
returnbuffer.array();
}
publicstaticlongbytesToLong(byte[]bytes){
buffer.put(bytes,0,bytes.length);
buffer.flip();//needflip
returnbuffer.getLong();
}
}
运行测试结果:
byte0=-22 int1=234 bytesInt=[B@de6ced int3=1417 bytes=[B@c17164 long2=2223
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。