java网络字节序转换关系.docx
- 文档编号:7513502
- 上传时间:2023-01-24
- 格式:DOCX
- 页数:10
- 大小:16.75KB
java网络字节序转换关系.docx
《java网络字节序转换关系.docx》由会员分享,可在线阅读,更多相关《java网络字节序转换关系.docx(10页珍藏版)》请在冰豆网上搜索。
java网络字节序转换关系
java网络字节序转换关系
工程项目开发时常常需要在java和c/c++之间进行socket通信,socket通信是以字节流或者字节包进行的,socket发送方须将数据转换为字节流或者字节包,而接收方则将字节流和字节包再转换回相应的数据类型。
如果发送方和接收方都是同种语言,则一般只涉及到字节序的调整。
而对于java和c/c++的通信,则情况就要复杂一些,主要是因为java中没有unsigned类型,并且java和c在某些数据类型上的长度不一致。
本文就是针对这种情况,整理了java数据类型和网络字节流或字节包(相当于java的byte数组)之间转换方法。
实际上网上这方面的资料不少,但往往不全,甚至有些有错误,于是就花了点时间对java整型数和网络字节序的byte[]之间转换的各种情况做了一些验证和整理。
整理出来的函数如下:
publicclassByteConvert{
//以下是整型数和网络字节序的 byte[]数组之间的转换
publicstaticbyte[]longToBytes(longn){
byte[]b=newbyte[8];
b[7]=(byte)(n&0xff);
b[6]=(byte)(n>>8 &0xff);
b[5]=(byte)(n>>16&0xff);
b[4]=(byte)(n>>24&0xff);
b[3]=(byte)(n>>32&0xff);
b[2]=(byte)(n>>40&0xff);
b[1]=(byte)(n>>48&0xff);
b[0]=(byte)(n>>56&0xff);
returnb;
}
publicstaticvoidlongToBytes(longn,byte[]array,intoffset){
array[7+offset]=(byte)(n&0xff);
array[6+offset]=(byte)(n>>8&0xff);
array[5+offset]=(byte)(n>>16&0xff);
array[4+offset]=(byte)(n>>24&0xff);
array[3+offset]=(byte)(n>>32&0xff);
array[2+offset]=(byte)(n>>40&0xff);
array[1+offset]=(byte)(n>>48&0xff);
array[0+offset]=(byte)(n>>56&0xff);
}
publicstaticlongbytesToLong(byte[]array)
{
return((((long)array[0]&0xff)<<56)
|(((long)array[1]&0xff)<<48)
|(((long)array[2]&0xff)<<40)
|(((long)array[3]&0xff)<<32)
|(((long)array[4]&0xff)<<24)
|(((long)array[5]&0xff)<<16)
|(((long)array[6]&0xff)<<8)
|(((long)array[7]&0xff)<<0));
}
publicstaticlongbytesToLong(byte[]array,intoffset)
{
return((((long)array[offset+0]&0xff)<<56)
|(((long)array[offset+1]&0xff)<<48)
|(((long)array[offset+2]&0xff)<<40)
|(((long)array[offset+3]&0xff)<<32)
|(((long)array[offset+4]&0xff)<<24)
|(((long)array[offset+5]&0xff)<<16)
|(((long)array[offset+6]&0xff)<<8)
|(((long)array[offset+7]&0xff)<<0));
}
publicstaticbyte[]intToBytes(intn){
byte[]b=newbyte[4];
b[3]=(byte)(n&0xff);
b[2]=(byte)(n>>8&0xff);
b[1]=(byte)(n>>16&0xff);
b[0]=(byte)(n>>24&0xff);
returnb;
}
publicstaticvoidintToBytes(intn,byte[]array,intoffset){
array[3+offset]=(byte)(n&0xff);
array[2+offset]=(byte)(n>>8&0xff);
array[1+offset]=(byte)(n>>16&0xff);
array[offset]=(byte)(n>>24&0xff);
}
publicstaticintbytesToInt(byteb[]){
return b[3]&0xff
|(b[2]&0xff)<<8
|(b[1]&0xff)<<16
|(b[0]&0xff)<<24;
}
publicstaticintbytesToInt(byteb[],intoffset){
return b[offset+3]&0xff
|(b[offset+2]&0xff)<<8
|(b[offset+1]&0xff)<<16
|(b[offset]&0xff)<<24;
}
publicstaticbyte[]uintToBytes(longn)
{
byte[]b=newbyte[4];
b[3]=(byte)(n&0xff);
b[2]=(byte)(n>>8&0xff);
b[1]=(byte)(n>>16&0xff);
b[0]=(byte)(n>>24&0xff);
returnb;
}
publicstaticvoiduintToBytes(longn,byte[]array,intoffset){
array[3+offset]=(byte)(n);
array[2+offset]=(byte)(n>>8&0xff);
array[1+offset]=(byte)(n>>16&0xff);
array[offset] =(byte)(n>>24&0xff);
}
publicstaticlongbytesToUint(byte[]array){
return((long)(array[3]&0xff))
|((long)(array[2]&0xff))<<8
|((long)(array[1]&0xff))<<16
|((long)(array[0]&0xff))<<24;
}
publicstaticlongbytesToUint(byte[]array,intoffset){
return((long)(array[offset+3]&0xff))
|((long)(array[offset+2]&0xff))<<8
|((long)(array[offset+1]&0xff))<<16
|((long)(array[offset] &0xff))<<24;
}
publicstaticbyte[]shortToBytes(shortn){
byte[]b=newbyte[2];
b[1]=(byte)(n &0xff);
b[0]=(byte)((n>>8)&0xff);
returnb;
}
publicstaticvoidshortToBytes(shortn,byte[]array,intoffset){
array[offset+1]=(byte)(n &0xff);
array[offset]=(byte)((n>>8)&0xff);
}
publicstaticshortbytesToShort(byte[]b){
return(short)(b[1]&0xff
|(b[0]&0xff)<<8);
}
publicstaticshortbytesToShort(byte[]b,intoffset){
return(short)(b[offset+1]&0xff
|(b[offset] &0xff)<<8);
}
publicstaticbyte[]ushortToBytes(intn){
byte[]b=newbyte[2];
b[1]=(byte)(n &0xff);
b[0]=(byte)((n>>8)&0xff);
returnb;
}
publicstaticvoidushortToBytes(intn,byte[]array,intoffset){
array[offset+1]=(byte)(n &0xff);
array[offset]=(byte) ((n>>8)&0xff);
}
publicstaticintbytesToUshort(byteb[]){
return b[1]&0xff
|(b[0]&0xff)<<8;
}
publicstaticintbytesToUshort(byteb[],intoffset){
return b[offset+1]&0xff
|(b[offset] &0xff)<<8;
}
publicstaticbyte[]ubyteToBytes(intn){
byte[]b=newbyte[1];
b[0]=(byte)(n&0xff);
returnb;
}
publicstaticvoidubyteToBytes(intn,byte[]array,intoffset){
array[0]=(byte)(n&0xff);
}
publicstaticintbytesToUbyte(byte[]array){
returnarray[0]&0xff;
}
publicstaticintbytesToUbyte(byte[]array,intoffset){
returnarray[offset]&0xff;
}
//char类型、float、double类型和byte[]数组之间的转换关系还需继续研究实现。
}
测试程序如下:
publicclassByteConvertTest{
publicstaticStringbyte2Hex(byte[]buf)
{
StringBufferstrbuf=newStringBuffer();
strbuf.append("{");
for(byteb:
buf)
{
if(b==0)
{
strbuf.append("00");
}
elseif(b==-1)
{
strbuf.append("FF");
}
else
{
Stringstr=Integer.toHexString(b).toUpperCase();
//sb.append(a);
if(str.length()==8)
{
str=str.substring(6,8);
}
elseif(str.length()<2)
{
str="0"+str;
}
strbuf.append(str);
}
strbuf.append("");
}
strbuf.append("}");
returnstrbuf.toString();
}
publicstaticbyte[]longToBytes(longn){
byte[]b=newbyte[8];
b[7]=(byte)(n&0xff);
b[6]=(byte)(n>>8 &0xff);
b[5]=(byte)(n>>16&0xff);
b[4]=(byte)(n>>24&0xff);
b[3]=(byte)(n>>32&0xff);
b[2]=(byte)(n>>40&0xff);
b[1]=(byte)(n>>48&0xff);
b[0]=(byte)(n>>56&0xff);
returnb;
}
publicstaticlongbytesToLong(byte[]array)
{
return((((long)array[0]&0xff)<<56)
|(((long)array[1]&0xff)<<48)
|(((long)array[2]&0xff)<<40)
|(((long)array[3]&0xff)<<32)
|(((long)array[4]&0xff)<<24)
|(((long)array[5]&0xff)<<16)
|(((long)array[6]&0xff)<<8)
|(((long)array[7]&0xff)));
}
publicstaticintbytesToInt(byteb[]){
return b[3]&0xff
|(b[2]&0xff)<<8
|(b[1]&0xff)<<16
|(b[0]&0xff)<<24;
}
publicstaticlongbytesToUint(byte[]array){
return((long)(array[3]&0xff))
|((long)(array[2]&0xff))<<8
|((long)(array[1]&0xff))<<16
|((long)(array[0]&0xff))<<24;
}
publicstaticbyte[]uintToBytes(longn)
{
byte[]b=newbyte[4];
b[3]=(byte)(n&0xff);
b[2]=(byte)(n>>8&0xff);
b[1]=(byte)(n>>16&0xff);
b[0]=(byte)(n>>24&0xff);
returnb;
}
publicstaticbyte[]shortToBytes(shortn){
byte[]b=newbyte[2];
b[1]=(byte)(n &0xff);
b[0]=(byte)((n>>8)&0xff);
returnb;
}
publicstaticshortbytesToShort(byte[]b){
return(short)(b[1]&0xff
|(b[0]&0xff)<<8);
}
staticvoidtestShortConvert(){
System.out.println("===================shortconvert=============");
System.out.println("byte2Hex(shortToBytes((short)0x11f2))"+byte2Hex(shortToBytes((short)0x11f2)));
System.out.print("println0x11f2:
");
System.out.println((short)0x11f2);
System.out.println("byte2Hex(shortToBytes((short)0xf1f2))"+byte2Hex(shortToBytes((short)0xf1f2)));
System.out.print("print
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 网络 字节 转换 关系