java整型数与网络字节序的 byte[] 数组转换关系
学习笔记,转自百度文库。
工作项目需要在java和c/c++之间进行socket通信,socket通信是以字节流或者字节包进行的,socket发送方须将数据转换为字节流或者字节包,而接收方则将字节流和字节包再转换回相应的数据类型。如果发送方和接收方都是同种语言,则一般只涉及到字节序的调整。而对于java和c/c++的通信,则情况就要复杂一些,主要是因为java中没有unsigned类型,并且java和c在某些数据类型上的长度不一致。
本文就是针对这种情况,整理了java数据类型和网络字节流或字节包(相当于java的byte数组)之间转换方法。实际上网上这方面的资料不少,但往往不全,甚至有些有错误,于是就花了点时间对java整型数和网络字节序的byte[]之间转换的各种情况做了一些验证和整理。整理出来的函数如下:
1 public class ByteConvert { 2 // 以下 是整型数 和 网络字节序的 byte[] 数组之间的转换 3 public static byte[] longToBytes(long n) { 4 byte[] b = new byte[8]; 5 b[7] = (byte) (n & 0xff); 6 b[6] = (byte) (n >> 8 & 0xff); 7 b[5] = (byte) (n >> 16 & 0xff); 8 b[4] = (byte) (n >> 24 & 0xff); 9 b[3] = (byte) (n >> 32 & 0xff); 10 b[2] = (byte) (n >> 40 & 0xff); 11 b[1] = (byte) (n >> 48 & 0xff); 12 b[0] = (byte) (n >> 56 & 0xff); 13 return b; 14 } 15 16 public static void longToBytes( long n, byte[] array, int offset ){ 17 array[7+offset] = (byte) (n & 0xff); 18 array[6+offset] = (byte) (n >> 8 & 0xff); 19 array[5+offset] = (byte) (n >> 16 & 0xff); 20 array[4+offset] = (byte) (n >> 24 & 0xff); 21 array[3+offset] = (byte) (n >> 32 & 0xff); 22 array[2+offset] = (byte) (n >> 40 & 0xff); 23 array[1+offset] = (byte) (n >> 48 & 0xff); 24 array[0+offset] = (byte) (n >> 56 & 0xff); 25 } 26 27 public static long bytesToLong( byte[] array ) 28 { 29 return ((((long) array[ 0] & 0xff) << 56) 30 | (((long) array[ 1] & 0xff) << 48) 31 | (((long) array[ 2] & 0xff) << 40) 32 | (((long) array[ 3] & 0xff) << 32) 33 | (((long) array[ 4] & 0xff) << 24) 34 | (((long) array[ 5] & 0xff) << 16) 35 | (((long) array[ 6] & 0xff) << 8) 36 | (((long) array[ 7] & 0xff) << 0)); 37 } 38 39 public static long bytesToLong( byte[] array, int offset ) 40 { 41 return ((((long) array[offset + 0] & 0xff) << 56) 42 | (((long) array[offset + 1] & 0xff) << 48) 43 | (((long) array[offset + 2] & 0xff) << 40) 44 | (((long) array[offset + 3] & 0xff) << 32) 45 | (((long) array[offset + 4] & 0xff) << 24) 46 | (((long) array[offset + 5] & 0xff) << 16) 47 | (((long) array[offset + 6] & 0xff) << 8) 48 | (((long) array[offset + 7] & 0xff) << 0)); 49 } 50 51 public static byte[] intToBytes(int n) { 52 byte[] b = new byte[4]; 53 b[3] = (byte) (n & 0xff); 54 b[2] = (byte) (n >> 8 & 0xff); 55 b[1] = (byte) (n >> 16 & 0xff); 56 b[0] = (byte) (n >> 24 & 0xff); 57 return b; 58 } 59 60 public static void intToBytes( int n, byte[] array, int offset ){ 61 array[3+offset] = (byte) (n & 0xff); 62 array[2+offset] = (byte) (n >> 8 & 0xff); 63 array[1+offset] = (byte) (n >> 16 & 0xff); 64 array[offset] = (byte) (n >> 24 & 0xff); 65 } 66 67 public static int bytesToInt(byte b[]) { 68 return b[3] & 0xff 69 | (b[2] & 0xff) << 8 70 | (b[1] & 0xff) << 16 71 | (b[0] & 0xff) << 24; 72 } 73 74 public static int bytesToInt(byte b[], int offset) { 75 return b[offset+3] & 0xff 76 | (b[offset+2] & 0xff) << 8 77 | (b[offset+1] & 0xff) << 16 78 | (b[offset] & 0xff) << 24; 79 } 80 81 public static byte[] uintToBytes( long n ) 82 { 83 byte[] b = new byte[4]; 84 b[3] = (byte) (n & 0xff); 85 b[2] = (byte) (n >> 8 & 0xff); 86 b[1] = (byte) (n >> 16 & 0xff); 87 b[0] = (byte) (n >> 24 & 0xff); 88 89 return b; 90 } 91 92 public static void uintToBytes( long n, byte[] array, int offset ){ 93 array[3+offset] = (byte) (n ); 94 array[2+offset] = (byte) (n >> 8 & 0xff); 95 array[1+offset] = (byte) (n >> 16 & 0xff); 96 array[offset] = (byte) (n >> 24 & 0xff); 97 } 98 99 public static long bytesToUint(byte[] array) { 100 return ((long) (array[3] & 0xff)) 101 | ((long) (array[2] & 0xff)) << 8 102 | ((long) (array[1] & 0xff)) << 16 103 | ((long) (array[0] & 0xff)) << 24; 104 } 105 106 public static long bytesToUint(byte[] array, int offset) { 107 return ((long) (array[offset+3] & 0xff)) 108 | ((long) (array[offset+2] & 0xff)) << 8 109 | ((long) (array[offset+1] & 0xff)) << 16 110 | ((long) (array[offset] & 0xff)) << 24; 111 } 112 113 public static byte[] shortToBytes(short n) { 114 byte[] b = new byte[2]; 115 b[1] = (byte) ( n & 0xff); 116 b[0] = (byte) ((n >> 8) & 0xff); 117 return b; 118 } 119 120 public static void shortToBytes(short n, byte[] array, int offset ) { 121 array[offset+1] = (byte) ( n & 0xff); 122 array[offset] = (byte) ((n >> 8) & 0xff); 123 } 124 125 public static short bytesToShort(byte[] b){ 126 return (short)( b[1] & 0xff 127 |(b[0] & 0xff) << 8 ); 128 } 129 130 public static short bytesToShort(byte[] b, int offset){ 131 return (short)( b[offset+1] & 0xff 132 |(b[offset] & 0xff) << 8 ); 133 } 134 135 public static byte[] ushortToBytes(int n) { 136 byte[] b = new byte[2]; 137 b[1] = (byte) ( n & 0xff); 138 b[0] = (byte) ((n >> 8) & 0xff); 139 return b; 140 } 141 142 public static void ushortToBytes(int n, byte[] array, int offset ) { 143 array[offset+1] = (byte) ( n & 0xff); 144 array[offset] = (byte) ((n >> 8) & 0xff); 145 } 146 147 public static int bytesToUshort(byte b[]) { 148 return b[1] & 0xff 149 | (b[0] & 0xff) << 8; 150 } 151 152 public static int bytesToUshort(byte b[], int offset) { 153 return b[offset+1] & 0xff 154 | (b[offset] & 0xff) << 8; 155 } 156 157 public static byte[] ubyteToBytes( int n ){ 158 byte[] b = new byte[1]; 159 b[0] = (byte) (n & 0xff); 160 return b; 161 } 162 163 public static void ubyteToBytes( int n, byte[] array, int offset ){ 164 array[0] = (byte) (n & 0xff); 165 } 166 167 public static int bytesToUbyte( byte[] array ){ 168 return array[0] & 0xff; 169 } 170 171 public static int bytesToUbyte( byte[] array, int offset ){ 172 return array[offset] & 0xff; 173 } 174 // char 类型、 float、double 类型和 byte[] 数组之间的转换关系还需继续研究实现。 175 } 176 177 178 测试程序如下: 179 180 public class ByteConvertTest { 181 182 public static String byte2Hex(byte[] buf) 183 { 184 StringBuffer strbuf = new StringBuffer(); 185 strbuf.append("{"); 186 for (byte b : buf) 187 { 188 if (b == 0) 189 { 190 strbuf.append("00"); 191 } 192 else if (b == -1) 193 { 194 strbuf.append("FF"); 195 } 196 else 197 { 198 String str = Integer.toHexString(b).toUpperCase(); 199 // sb.append(a); 200 if (str.length() == 8) 201 { 202 str = str.substring(6, 8); 203 } 204 else if (str.length() < 2) 205 { 206 str = "0" + str; 207 } 208 strbuf.append(str); 209 } 210 strbuf.append(" "); 211 } 212 strbuf.append("}"); 213 return strbuf.toString(); 214 } 215 216 public static byte[] longToBytes(long n) { 217 byte[] b = new byte[8]; 218 b[7] = (byte) (n & 0xff); 219 b[6] = (byte) (n >> 8 & 0xff); 220 b[5] = (byte) (n >> 16 & 0xff); 221 b[4] = (byte) (n >> 24 & 0xff); 222 b[3] = (byte) (n >> 32 & 0xff); 223 b[2] = (byte) (n >> 40 & 0xff); 224 b[1] = (byte) (n >> 48 & 0xff); 225 b[0] = (byte) (n >> 56 & 0xff); 226 return b; 227 } 228 229 public static long bytesToLong( byte[] array ) 230 { 231 return ((((long) array[ 0] & 0xff) << 56) 232 | (((long) array[ 1] & 0xff) << 48) 233 | (((long) array[ 2] & 0xff) << 40) 234 | (((long) array[ 3] & 0xff) << 32) 235 | (((long) array[ 4] & 0xff) << 24) 236 | (((long) array[ 5] & 0xff) << 16) 237 | (((long) array[ 6] & 0xff) << 8) 238 | (((long) array[ 7] & 0xff) )); 239 } 240 241 public static int bytesToInt(byte b[]) { 242 return b[3] & 0xff 243 | (b[2] & 0xff) << 8 244 | (b[1] & 0xff) << 16 245 | (b[0] & 0xff) << 24; 246 } 247 248 public static long bytesToUint(byte[] array) { 249 return ((long) (array[3] & 0xff)) 250 | ((long) (array[2] & 0xff)) << 8 251 | ((long) (array[1] & 0xff)) << 16 252 | ((long) (array[0] & 0xff)) << 24; 253 } 254 255 public static byte[] uintToBytes( long n ) 256 { 257 byte[] b = new byte[4]; 258 b[3] = (byte) (n & 0xff); 259 b[2] = (byte) (n >> 8 & 0xff); 260 b[1] = (byte) (n >> 16 & 0xff); 261 b[0] = (byte) (n >> 24 & 0xff); 262 263 return b; 264 } 265 266 267 public static byte[] shortToBytes(short n) { 268 byte[] b = new byte[2]; 269 b[1] = (byte) ( n & 0xff); 270 b[0] = (byte) ((n >> 8) & 0xff); 271 return b; 272 } 273 274 public static short bytesToShort(byte[] b){ 275 return (short)( b[1] & 0xff 276 |(b[0] & 0xff) << 8 ); 277 } 278 279 static void testShortConvert(){ 280 System.out.println("=================== short convert ============="); 281 System.out.println("byte2Hex(shortToBytes((short)0x11f2))"+byte2Hex(shortToBytes((short)0x11f2))); 282 System.out.print("println 0x11f2:"); 283 System.out.println((short)0x11f2); 284 System.out.println("byte2Hex(shortToBytes((short)0xf1f2))"+byte2Hex(shortToBytes((short)0xf1f2))); 285 System.out.print("println 0xf1f2:"); 286 System.out.println((short)0xf1f2); 287 System.out.print("println bytesToShort(shortToBytes((short)0x11f2)):"); 288 System.out.println((short)bytesToShort(shortToBytes((short)0x11f2))); 289 System.out.print("println bytesToShort(shortToBytes((short)0xf1f2)):"); 290 System.out.println((short)bytesToShort(shortToBytes((short)0xf1f2))); 291 } 292 293 294 public static byte[] ushortToBytes(int n) { 295 byte[] b = new byte[2]; 296 b[1] = (byte) (n & 0xff); 297 b[0] = (byte) (n >> 8 & 0xff); 298 return b; 299 } 300 301 302 public static int bytesToUshort(byte b[]) { 303 return b[1] & 0xff 304 | (b[0] & 0xff) << 8; 305 } 306 307 static void testUshortConvert(){ 308 System.out.println("=================== Ushort convert ============="); 309 System.out.println("byte2Hex(ushortToBytes(0x11f2))"+byte2Hex(ushortToBytes(0x11f2))); 310 System.out.print("println 0x11f2:"); 311 System.out.println(0x11f2); 312 System.out.println("byte2Hex(ushortToBytes(0xf1f2))"+byte2Hex(ushortToBytes(0xf1f2))); 313 System.out.print("println 0xf1f2:"); 314 System.out.println(0xf1f2); 315 System.out.print("println bytesToUshort(ushortToBytes(0x11f2)):"); 316 System.out.println(bytesToUshort(ushortToBytes(0x11f2))); 317 System.out.print("println bytesToUshort(ushortToBytes(0xf1f2)):"); 318 System.out.println(bytesToUshort(ushortToBytes(0xf1f2))); 319 } 320 321 public static byte[] ubyteToBytes( int n ){ 322 byte[] b = new byte[1]; 323 b[0] = (byte) (n & 0xff); 324 return b; 325 } 326 327 public static int bytesToUbyte( byte[] array ){ 328 return array[0] & 0xff; 329 } 330 331 static void testUbyteConvert(){ 332 System.out.println("=================== Ubyte convert ============="); 333 System.out.println("byte2Hex(ubyteToBytes(0x1112))"+byte2Hex(ubyteToBytes(0x1112))); 334 System.out.print("println 0x1112:"); 335 System.out.println(0x1112); 336 System.out.println("byte2Hex(ubyteToBytes(0xf2))"+byte2Hex(ubyteToBytes(0xf2))); 337 System.out.print("println 0xf2:"); 338 System.out.println(0xf2); 339 System.out.print("println bytesToUbyte(ubyteToBytes(0x1112)):"); 340 System.out.println(bytesToUbyte(ubyteToBytes(0x1112))); 341 System.out.print("println bytesToUbyte(ubyteToBytes(0xf1f2)):"); 342 System.out.println(bytesToUbyte(ubyteToBytes(0xf1f2))); 343 } 344 345 346 /** 347 * @param args 348 */ 349 public static void main(String[] args) { 350 // TODO Auto-generated method stub 351 byte[] array = new byte[4]; 352 array[3] = (byte) 0xF4; 353 array[2] = 0x13; 354 array[1] = 0x12; 355 array[0] = 0x11; 356 357 System.out.println("=================== Integer bytes ============="); 358 359 System.out.println("the bytes is:"+byte2Hex(array) ); 360 System.out.print("println bytesToInt :"); 361 System.out.println( bytesToInt(array)); 362 System.out.printf("printf bytesToInt :%X ", bytesToInt(array)); 363 364 System.out.println("=================== long bytes ============="); 365 byte[] longBytes = new byte[8]; 366 367 longBytes[7] = (byte) 0xf7; 368 longBytes[6] = (byte) 0x16; 369 longBytes[5] = (byte) 0xf5; 370 longBytes[4] = (byte) 0x14; 371 longBytes[3] = (byte) 0xf3; 372 longBytes[2] = (byte) 0x12; 373 longBytes[1] = (byte) 0xf1; 374 longBytes[0] = (byte) 0x10; 375 376 377 System.out.println( "the bytes is:"+byte2Hex(longBytes) ); 378 System.out.printf("printf bytesToLong:%X ",bytesToLong(longBytes)); 379 380 System.out.println("=================byte to long ================"); 381 382 byte b = (byte)0xf1; 383 System.out.print("Println the byte:"); 384 System.out.println(b); 385 System.out.printf("Printf the byte:%X ",b); 386 long l = b; 387 System.out.print("Println byte to long:"); 388 System.out.println(l); 389 System.out.printf("printf byte to long:%X ",l); 390 391 System.out.println("================= uint Bytes ================"); 392 393 byte[] uint = new byte[4]; 394 uint[3] = (byte) 0xf3; 395 uint[2] = (byte) 0x12; 396 uint[1] = (byte) 0xf1; 397 uint[0] = (byte) 0xFF; 398 399 System.out.println( "the bytes is:"+byte2Hex(uint) ); 400 System.out.printf("printf bytesToUint:%X ",bytesToUint(uint)); 401 System.out.print("Println bytesToUint:"); 402 System.out.println(bytesToUint(uint)); 403 System.out.println("byte2Hex(uintToBytes(0x11f2f3f4f5f6f7f8l)):"+byte2Hex(uintToBytes(0x11f2f3f4f5f6f7f8l))); 404 405 System.out.println("===============Long Integer=============="); 406 System.out.print("println 0x11f2f3f4f5f6f7f8l:"); 407 System.out.println(0x11f2f3f4f5f6f7f8l); 408 System.out.printf("Printf 0x11f2f3f4f5f6f7f8l:%X ",0x11f2f3f4f5f6f7f8l); 409 System.out.println("println byte2Hex(longToBytes(0x11f2f3f4f5f6f7f8l))"+byte2Hex(longToBytes(0x11f2f3f4f5f6f7f8l))); 410 // 注意,下面的这行,并不能获得正确的uint。 411 System.out.printf("printf bytesToUint(longToBytes(0x11f2f3f4f5f6f7f8l):%X ",bytesToUint(longToBytes(0x11f2f3f4f5f6f7f8l))); 412 413 System.out.println("===============bytesToLong(longToBytes())=============="); 414 System.out.println(bytesToLong(longToBytes(0x11f2f3f4f5f6f7f8l))); 415 System.out.printf("%X ",bytesToLong(longToBytes(0x11f2f3f4f5f6f7f8l))); 416 417 testShortConvert(); 418 testUshortConvert(); 419 testUbyteConvert(); 420 } 421 422 }