java网络字节序转换关系Word格式.docx
《java网络字节序转换关系Word格式.docx》由会员分享,可在线阅读,更多相关《java网络字节序转换关系Word格式.docx(10页珍藏版)》请在冰豆网上搜索。
&
b[5]=(byte)(n>
16&
b[4]=(byte)(n>
24&
b[3]=(byte)(n>
32&
b[2]=(byte)(n>
40&
b[1]=(byte)(n>
48&
b[0]=(byte)(n>
56&
returnb;
}
publicstaticvoidlongToBytes(longn,byte[]array,intoffset){
array[7+offset]=(byte)(n&
array[6+offset]=(byte)(n>
8&
array[5+offset]=(byte)(n>
array[4+offset]=(byte)(n>
array[3+offset]=(byte)(n>
array[2+offset]=(byte)(n>
array[1+offset]=(byte)(n>
array[0+offset]=(byte)(n>
publicstaticlongbytesToLong(byte[]array)
{
return((((long)array[0]&
0xff)<
<
56)
|(((long)array[1]&
48)
|(((long)array[2]&
40)
|(((long)array[3]&
32)
|(((long)array[4]&
24)
|(((long)array[5]&
16)
|(((long)array[6]&
8)
|(((long)array[7]&
0));
publicstaticlongbytesToLong(byte[]array,intoffset)
return((((long)array[offset+0]&
|(((long)array[offset+1]&
|(((long)array[offset+2]&
|(((long)array[offset+3]&
|(((long)array[offset+4]&
|(((long)array[offset+5]&
|(((long)array[offset+6]&
|(((long)array[offset+7]&
publicstaticbyte[]intToBytes(intn){
byte[]b=newbyte[4];
b[3]=(byte)(n&
publicstaticvoidintToBytes(intn,byte[]array,intoffset){
array[3+offset]=(byte)(n&
array[offset]=(byte)(n>
}
publicstaticintbytesToInt(byteb[]){
return
b[3]&
0xff
|(b[2]&
8
|(b[1]&
16
|(b[0]&
24;
publicstaticintbytesToInt(byteb[],intoffset){
b[offset+3]&
|(b[offset+2]&
|(b[offset+1]&
|(b[offset]&
publicstaticbyte[]uintToBytes(longn)
publicstaticvoiduintToBytes(longn,byte[]array,intoffset){
array[3+offset]=(byte)(n);
array[offset]
=(byte)(n>
publicstaticlongbytesToUint(byte[]array){
return((long)(array[3]&
0xff))
|((long)(array[2]&
0xff))<
|((long)(array[1]&
16
|((long)(array[0]&
publicstaticlongbytesToUint(byte[]array,intoffset){
return((long)(array[offset+3]&
|((long)(array[offset+2]&
|((long)(array[offset+1]&
|((long)(array[offset]
publicstaticbyte[]shortToBytes(shortn){
byte[]b=newbyte[2];
b[1]=(byte)(n
b[0]=(byte)((n>
8)&
publicstaticvoidshortToBytes(shortn,byte[]array,intoffset){
array[offset+1]=(byte)(n
array[offset]=(byte)((n>
publicstaticshortbytesToShort(byte[]b){
return(short)(b[1]&
0xff
|(b[0]&
8);
publicstaticshortbytesToShort(byte[]b,intoffset){
return(short)(b[offset+1]&
|(b[offset]
publicstaticbyte[]ushortToBytes(intn){
publicstaticvoidushortToBytes(intn,byte[]array,intoffset){
array[offset]=(byte)
((n>
publicstaticintbytesToUshort(byteb[]){
b[1]&
8;
publicstaticintbytesToUshort(byteb[],intoffset){
b[offset+1]&
|(b[offset]
publicstaticbyte[]ubyteToBytes(intn){
byte[]b=newbyte[1];
b[0]=(byte)(n&
publicstaticvoidubyteToBytes(intn,byte[]array,intoffset){
array[0]=(byte)(n&
publicstaticintbytesToUbyte(byte[]array){
returnarray[0]&
0xff;
publicstaticintbytesToUbyte(byte[]array,intoffset){
returnarray[offset]&
//char类型、float、double类型和byte[]数组之间的转换关系还需继续研究实现。
}
测试程序如下:
publicclassByteConvertTest{
publicstaticStringbyte2Hex(byte[]buf)
StringBufferstrbuf=newStringBuffer();
strbuf.append("
{"
);
for(byteb:
buf)
if(b==0)
00"
}
elseif(b==-1)
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);
"
}"
returnstrbuf.toString();
0xff)));
staticvoidtestShortConvert(){
System.out.println("
===================shortconvert============="
byte2Hex(shortToBytes((short)0x11f2))"
+byte2Hex(shortToBytes((short)0x11f2)));
System.out.print("
println0x11f2:
"
System.out.println((short)0x11f2);
byte2Hex(shortToBytes((short)0xf1f2))"
+byte2Hex(shortToBytes((short)0xf1f2)));
print