java 网络序_java整型数与网络字节序的 byte[] 数组转换关系

1 public classByteConvert {2 //以下 是整型数 和 网络字节序的 byte[] 数组之间的转换

3 public static byte[] longToBytes(longn) {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 returnb;14 }15

16 public static void longToBytes( long n, byte[] array, intoffset ){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, intoffset )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(intn) {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 returnb;58 }59

60 public static void intToBytes( int n, byte[] array, intoffset ){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(byteb[]) {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[], intoffset) {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( longn )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 returnb;90 }91

92 public static void uintToBytes( long n, byte[] array, intoffset ){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, intoffset) {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(shortn) {114 byte[] b = new byte[2];115 b[1] = (byte) ( n & 0xff);116 b[0] = (byte) ((n >> 8) & 0xff);117 returnb;118 }119

120 public static void shortToBytes(short n, byte[] array, intoffset ) {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, intoffset){131 return (short)( b[offset+1] & 0xff

132 |(b[offset] & 0xff) << 8);133 }134

135 public static byte[] ushortToBytes(intn) {136 byte[] b = new byte[2];137 b[1] = (byte) ( n & 0xff);138 b[0] = (byte) ((n >> 8) & 0xff);139 returnb;140 }141

142 public static void ushortToBytes(int n, byte[] array, intoffset ) {143 array[offset+1] = (byte) ( n & 0xff);144 array[offset] = (byte) ((n >> 8) & 0xff);145 }146

147 public static int bytesToUshort(byteb[]) {148 return b[1] & 0xff

149 | (b[0] & 0xff) << 8;150 }151

152 public static int bytesToUshort(byte b[], intoffset) {153 return b[offset+1] & 0xff

154 | (b[offset] & 0xff) << 8;155 }156

157 public static byte[] ubyteToBytes( intn ){158 byte[] b = new byte[1];159 b[0] = (byte) (n & 0xff);160 returnb;161 }162

163 public static void ubyteToBytes( int n, byte[] array, intoffset ){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, intoffset ){172 return array[offset] & 0xff;173 }174 //char 类型、 float、double 类型和 byte[] 数组之间的转换关系还需继续研究实现。

175 }176

177

178 测试程序如下:179

180 public classByteConvertTest {181

182 public static String byte2Hex(byte[] buf)183 {184 StringBuffer strbuf = newStringBuffer();185 strbuf.append("{");186 for (byteb : 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 returnstrbuf.toString();214 }215

216 public static byte[] longToBytes(longn) {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 returnb;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(byteb[]) {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( longn )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 returnb;264 }265

266

267 public static byte[] shortToBytes(shortn) {268 byte[] b = new byte[2];269 b[1] = (byte) ( n & 0xff);270 b[0] = (byte) ((n >> 8) & 0xff);271 returnb;272 }273

274 public static short bytesToShort(byte[] b){275 return (short)( b[1] & 0xff

276 |(b[0] & 0xff) << 8);277 }278

279 static voidtestShortConvert(){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(intn) {295 byte[] b = new byte[2];296 b[1] = (byte) (n & 0xff);297 b[0] = (byte) (n >> 8 & 0xff);298 returnb;299 }300

301

302 public static int bytesToUshort(byteb[]) {303 return b[1] & 0xff

304 | (b[0] & 0xff) << 8;305 }306

307 static voidtestUshortConvert(){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( intn ){322 byte[] b = new byte[1];323 b[0] = (byte) (n & 0xff);324 returnb;325 }326

327 public static int bytesToUbyte( byte[] array ){328 return array[0] & 0xff;329 }330

331 static voidtestUbyteConvert(){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 *@paramargs348 */

349 public static voidmain(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\n", 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\n",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\n",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\n",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\n",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\n",0x11f2f3f4f5f6f7f8l);409 System.out.println("println byte2Hex(longToBytes(0x11f2f3f4f5f6f7f8l))"+byte2Hex(longToBytes(0x11f2f3f4f5f6f7f8l)));410 //注意,下面的这行,并不能获得正确的uint。

411 System.out.printf("printf bytesToUint(longToBytes(0x11f2f3f4f5f6f7f8l):%X\n",bytesToUint(longToBytes(0x11f2f3f4f5f6f7f8l)));412

413 System.out.println("===============bytesToLong(longToBytes())==============");414 System.out.println(bytesToLong(longToBytes(0x11f2f3f4f5f6f7f8l)));415 System.out.printf("%X\n",bytesToLong(longToBytes(0x11f2f3f4f5f6f7f8l)));416

417 testShortConvert();418 testUshortConvert();419 testUbyteConvert();420 }421

422 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值