Java整型数与网络字节序byte[]数组转换关系详解
程序员文章站
2024-02-26 12:10:40
本文实例讲述了java整型数与网络字节序byte[]数组转换关系。分享给大家供大家参考,具体如下:
工作项目需要在java和c/c++之间进行socket通信,socke...
本文实例讲述了java整型数与网络字节序byte[]数组转换关系。分享给大家供大家参考,具体如下:
工作项目需要在java和c/c++之间进行socket通信,socket通信是以字节流或者字节包进行的,socket发送方须将数据转换为字节流或者字节包,而接收方则将字节流和字节包再转换回相应的数据类型。如果发送方和接收方都是同种语言,则一般只涉及到字节序的调整。而对于java和c/c++的通信,则情况就要复杂一些,主要是因为java中没有unsigned类型,并且java和c在某些数据类型上的长度不一致。
针对这种情况,本文整理了java数据类型和网络字节流或字节包(相当于java的byte数组)之间转换方法。实际上网上这方面的资料不少,但往往不全,甚至有些有错误,于是就花了点时间对java整型数和网络字节序的byte[]之间转换的各种情况做了一些验证和整理。整理出来的函数如下:
public class byteconvert { // 以下 是整型数 和 网络字节序的 byte[] 数组之间的转换 public static byte[] longtobytes(long n) { byte[] b = new byte[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); return b; } public static void longtobytes( long n, byte[] array, int offset ){ 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); } public static long bytestolong( 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)); } public static long bytestolong( byte[] array, int offset ) { 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)); } public static byte[] inttobytes(int n) { byte[] b = new byte[4]; b[3] = (byte) (n & 0xff); b[2] = (byte) (n >> 8 & 0xff); b[1] = (byte) (n >> 16 & 0xff); b[0] = (byte) (n >> 24 & 0xff); return b; } public static void inttobytes( int n, byte[] array, int offset ){ 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); } public static int bytestoint(byte b[]) { return b[3] & 0xff | (b[2] & 0xff) << 8 | (b[1] & 0xff) << 16 | (b[0] & 0xff) << 24; } public static int bytestoint(byte b[], int offset) { return b[offset+3] & 0xff | (b[offset+2] & 0xff) << 8 | (b[offset+1] & 0xff) << 16 | (b[offset] & 0xff) << 24; } public static byte[] uinttobytes( long n ) { byte[] b = new byte[4]; b[3] = (byte) (n & 0xff); b[2] = (byte) (n >> 8 & 0xff); b[1] = (byte) (n >> 16 & 0xff); b[0] = (byte) (n >> 24 & 0xff); return b; } public static void uinttobytes( long n, byte[] array, int offset ){ 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); } public static long bytestouint(byte[] array) { return ((long) (array[3] & 0xff)) | ((long) (array[2] & 0xff)) << 8 | ((long) (array[1] & 0xff)) << 16 | ((long) (array[0] & 0xff)) << 24; } public static long bytestouint(byte[] array, int offset) { return ((long) (array[offset+3] & 0xff)) | ((long) (array[offset+2] & 0xff)) << 8 | ((long) (array[offset+1] & 0xff)) << 16 | ((long) (array[offset] & 0xff)) << 24; } public static byte[] shorttobytes(short n) { byte[] b = new byte[2]; b[1] = (byte) ( n & 0xff); b[0] = (byte) ((n >> 8) & 0xff); return b; } public static void shorttobytes(short n, byte[] array, int offset ) { array[offset+1] = (byte) ( n & 0xff); array[offset] = (byte) ((n >> 8) & 0xff); } public static short bytestoshort(byte[] b){ return (short)( b[1] & 0xff |(b[0] & 0xff) << 8 ); } public static short bytestoshort(byte[] b, int offset){ return (short)( b[offset+1] & 0xff |(b[offset] & 0xff) << 8 ); } public static byte[] ushorttobytes(int n) { byte[] b = new byte[2]; b[1] = (byte) ( n & 0xff); b[0] = (byte) ((n >> 8) & 0xff); return b; } public static void ushorttobytes(int n, byte[] array, int offset ) { array[offset+1] = (byte) ( n & 0xff); array[offset] = (byte) ((n >> 8) & 0xff); } public static int bytestoushort(byte b[]) { return b[1] & 0xff | (b[0] & 0xff) << 8; } public static int bytestoushort(byte b[], int offset) { return b[offset+1] & 0xff | (b[offset] & 0xff) << 8; } public static byte[] ubytetobytes( int n ){ byte[] b = new byte[1]; b[0] = (byte) (n & 0xff); return b; } public static void ubytetobytes( int n, byte[] array, int offset ){ array[0] = (byte) (n & 0xff); } public static int bytestoubyte( byte[] array ){ return array[0] & 0xff; } public static int bytestoubyte( byte[] array, int offset ){ return array[offset] & 0xff; } // char 类型、 float、double 类型和 byte[] 数组之间的转换关系还需继续研究实现。 }
测试程序如下:
public class byteconverttest { public static string byte2hex(byte[] buf) { stringbuffer strbuf = new stringbuffer(); strbuf.append("{"); for (byte b : buf) { if (b == 0) { strbuf.append("00"); } else if (b == -1) { strbuf.append("ff"); } else { string str = integer.tohexstring(b).touppercase(); // sb.append(a); if (str.length() == 8) { str = str.substring(6, 8); } else if (str.length() < 2) { str = "0" + str; } strbuf.append(str); } strbuf.append(" "); } strbuf.append("}"); return strbuf.tostring(); } public static byte[] longtobytes(long n) { byte[] b = new byte[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); return b; } public static long bytestolong( 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) )); } public static int bytestoint(byte b[]) { return b[3] & 0xff | (b[2] & 0xff) << 8 | (b[1] & 0xff) << 16 | (b[0] & 0xff) << 24; } public static long bytestouint(byte[] array) { return ((long) (array[3] & 0xff)) | ((long) (array[2] & 0xff)) << 8 | ((long) (array[1] & 0xff)) << 16 | ((long) (array[0] & 0xff)) << 24; } public static byte[] uinttobytes( long n ) { byte[] b = new byte[4]; b[3] = (byte) (n & 0xff); b[2] = (byte) (n >> 8 & 0xff); b[1] = (byte) (n >> 16 & 0xff); b[0] = (byte) (n >> 24 & 0xff); return b; } public static byte[] shorttobytes(short n) { byte[] b = new byte[2]; b[1] = (byte) ( n & 0xff); b[0] = (byte) ((n >> 8) & 0xff); return b; } public static short bytestoshort(byte[] b){ return (short)( b[1] & 0xff |(b[0] & 0xff) << 8 ); } static void testshortconvert(){ system.out.println("=================== short convert ============="); system.out.println("byte2hex(shorttobytes((short)0x11f2))"+byte2hex(shorttobytes((short)0x11f2))); system.out.print("println 0x11f2:"); system.out.println((short)0x11f2); system.out.println("byte2hex(shorttobytes((short)0xf1f2))"+byte2hex(shorttobytes((short)0xf1f2))); system.out.print("println 0xf1f2:"); system.out.println((short)0xf1f2); system.out.print("println bytestoshort(shorttobytes((short)0x11f2)):"); system.out.println((short)bytestoshort(shorttobytes((short)0x11f2))); system.out.print("println bytestoshort(shorttobytes((short)0xf1f2)):"); system.out.println((short)bytestoshort(shorttobytes((short)0xf1f2))); } public static byte[] ushorttobytes(int n) { byte[] b = new byte[2]; b[1] = (byte) (n & 0xff); b[0] = (byte) (n >> 8 & 0xff); return b; } public static int bytestoushort(byte b[]) { return b[1] & 0xff | (b[0] & 0xff) << 8; } static void testushortconvert(){ system.out.println("=================== ushort convert ============="); system.out.println("byte2hex(ushorttobytes(0x11f2))"+byte2hex(ushorttobytes(0x11f2))); system.out.print("println 0x11f2:"); system.out.println(0x11f2); system.out.println("byte2hex(ushorttobytes(0xf1f2))"+byte2hex(ushorttobytes(0xf1f2))); system.out.print("println 0xf1f2:"); system.out.println(0xf1f2); system.out.print("println bytestoushort(ushorttobytes(0x11f2)):"); system.out.println(bytestoushort(ushorttobytes(0x11f2))); system.out.print("println bytestoushort(ushorttobytes(0xf1f2)):"); system.out.println(bytestoushort(ushorttobytes(0xf1f2))); } public static byte[] ubytetobytes( int n ){ byte[] b = new byte[1]; b[0] = (byte) (n & 0xff); return b; } public static int bytestoubyte( byte[] array ){ return array[0] & 0xff; } static void testubyteconvert(){ system.out.println("=================== ubyte convert ============="); system.out.println("byte2hex(ubytetobytes(0x1112))"+byte2hex(ubytetobytes(0x1112))); system.out.print("println 0x1112:"); system.out.println(0x1112); system.out.println("byte2hex(ubytetobytes(0xf2))"+byte2hex(ubytetobytes(0xf2))); system.out.print("println 0xf2:"); system.out.println(0xf2); system.out.print("println bytestoubyte(ubytetobytes(0x1112)):"); system.out.println(bytestoubyte(ubytetobytes(0x1112))); system.out.print("println bytestoubyte(ubytetobytes(0xf1f2)):"); system.out.println(bytestoubyte(ubytetobytes(0xf1f2))); } /** * @param args */ public static void main(string[] args) { // todo auto-generated method stub byte[] array = new byte[4]; array[3] = (byte) 0xf4; array[2] = 0x13; array[1] = 0x12; array[0] = 0x11; system.out.println("=================== integer bytes ============="); system.out.println("the bytes is:"+byte2hex(array) ); system.out.print("println bytestoint :"); system.out.println( bytestoint(array)); system.out.printf("printf bytestoint :%x\n", bytestoint(array)); system.out.println("=================== long bytes ============="); byte[] longbytes = new byte[8]; longbytes[7] = (byte) 0xf7; longbytes[6] = (byte) 0x16; longbytes[5] = (byte) 0xf5; longbytes[4] = (byte) 0x14; longbytes[3] = (byte) 0xf3; longbytes[2] = (byte) 0x12; longbytes[1] = (byte) 0xf1; longbytes[0] = (byte) 0x10; system.out.println( "the bytes is:"+byte2hex(longbytes) ); system.out.printf("printf bytestolong:%x\n",bytestolong(longbytes)); system.out.println("=================byte to long ================"); byte b = (byte)0xf1; system.out.print("println the byte:"); system.out.println(b); system.out.printf("printf the byte:%x\n",b); long l = b; system.out.print("println byte to long:"); system.out.println(l); system.out.printf("printf byte to long:%x\n",l); system.out.println("================= uint bytes ================"); byte[] uint = new byte[4]; uint[3] = (byte) 0xf3; uint[2] = (byte) 0x12; uint[1] = (byte) 0xf1; uint[0] = (byte) 0xff; system.out.println( "the bytes is:"+byte2hex(uint) ); system.out.printf("printf bytestouint:%x\n",bytestouint(uint)); system.out.print("println bytestouint:"); system.out.println(bytestouint(uint)); system.out.println("byte2hex(uinttobytes(0x11f2f3f4f5f6f7f8l)):"+byte2hex(uinttobytes(0x11f2f3f4f5f6f7f8l))); system.out.println("===============long integer=============="); system.out.print("println 0x11f2f3f4f5f6f7f8l:"); system.out.println(0x11f2f3f4f5f6f7f8l); system.out.printf("printf 0x11f2f3f4f5f6f7f8l:%x\n",0x11f2f3f4f5f6f7f8l); system.out.println("println byte2hex(longtobytes(0x11f2f3f4f5f6f7f8l))"+byte2hex(longtobytes(0x11f2f3f4f5f6f7f8l))); // 注意,下面的这行,并不能获得正确的uint。 system.out.printf("printf bytestouint(longtobytes(0x11f2f3f4f5f6f7f8l):%x\n",bytestouint(longtobytes(0x11f2f3f4f5f6f7f8l))); system.out.println("===============bytestolong(longtobytes())=============="); system.out.println(bytestolong(longtobytes(0x11f2f3f4f5f6f7f8l))); system.out.printf("%x\n",bytestolong(longtobytes(0x11f2f3f4f5f6f7f8l))); testshortconvert(); testushortconvert(); testubyteconvert(); } }
更多关于java相关内容感兴趣的读者可查看本站专题:《java字符与字符串操作技巧总结》、《java数学运算技巧总结》、《java数据结构与算法教程》、《java操作dom节点技巧总结》和《java数组操作技巧总结》
希望本文所述对大家java程序设计有所帮助。