Java整型數(shù)與網(wǎng)絡(luò)字節(jié)序byte[]數(shù)組轉(zhuǎn)換關(guān)系詳解
本文實(shí)例講述了Java整型數(shù)與網(wǎng)絡(luò)字節(jié)序byte[]數(shù)組轉(zhuǎn)換關(guān)系。分享給大家供大家參考,具體如下:
工作項(xiàng)目需要在java和c/c++之間進(jìn)行socket通信,socket通信是以字節(jié)流或者字節(jié)包進(jìn)行的,socket發(fā)送方須將數(shù)據(jù)轉(zhuǎn)換為字節(jié)流或者字節(jié)包,而接收方則將字節(jié)流和字節(jié)包再轉(zhuǎn)換回相應(yīng)的數(shù)據(jù)類型。如果發(fā)送方和接收方都是同種語言,則一般只涉及到字節(jié)序的調(diào)整。而對于java和c/c++的通信,則情況就要復(fù)雜一些,主要是因?yàn)閖ava中沒有unsigned類型,并且java和c在某些數(shù)據(jù)類型上的長度不一致。
針對這種情況,本文整理了java數(shù)據(jù)類型和網(wǎng)絡(luò)字節(jié)流或字節(jié)包(相當(dāng)于java的byte數(shù)組)之間轉(zhuǎn)換方法。實(shí)際上網(wǎng)上這方面的資料不少,但往往不全,甚至有些有錯(cuò)誤,于是就花了點(diǎn)時(shí)間對java整型數(shù)和網(wǎng)絡(luò)字節(jié)序的byte[]之間轉(zhuǎn)換的各種情況做了一些驗(yàn)證和整理。整理出來的函數(shù)如下:
public class ByteConvert { // 以下 是整型數(shù) 和 網(wǎng)絡(luò)字節(jié)序的 byte[] 數(shù)組之間的轉(zhuǎn)換 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[] 數(shù)組之間的轉(zhuǎn)換關(guān)系還需繼續(xù)研究實(shí)現(xiàn)。 }
測試程序如下:
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(); } }
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java字符與字符串操作技巧總結(jié)》、《Java數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java數(shù)組操作技巧總結(jié)》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
- 在剛開始學(xué)習(xí)Java的時(shí)候,就了解了Java基礎(chǔ)中的變量,雖然知道這個(gè)以后會經(jīng)常用到,但沒想到了基本語法這里,竟然又冒出來了成員變量和局部變量。變來變?nèi)ヌ菀鬃屓烁銜灹耍裉煳覀兙吞魭鰜硎崂硪幌拢?/div> 2016-07-07
JDK17、JDK19、JDK1.8輕松切換(無坑版,小白也可以看懂!)
在做不同的java項(xiàng)目時(shí)候,因項(xiàng)目需要很可能來回切換jdk版本,下面這篇文章主要介紹了JDK17、JDK19、JDK1.8輕松切換的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02Java避免死鎖_動力節(jié)點(diǎn)Java學(xué)院整理
在有些情況下死鎖是可以避免的。本文將展示三種用于避免死鎖的技術(shù)。對java避免死鎖的相關(guān)知識感興趣的朋友一起通過本文學(xué)習(xí)吧2017-06-06提示:Decompiled.class file,bytecode version如何解決
在處理Decompiled.classfile和bytecodeversion問題時(shí),通過修改Maven配置文件,添加阿里云鏡像并去掉默認(rèn)鏡像,解決了下載源的問題,同時(shí),檢查并修改了依賴版本,確保了問題的解決2024-12-12用intellij Idea加載eclipse的maven項(xiàng)目全流程(圖文)
這篇文章主要介紹了用intellij Idea加載eclipse的maven項(xiàng)目全流程(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12Java利用redis zset實(shí)現(xiàn)延時(shí)任務(wù)詳解
zset作為redis的有序集合數(shù)據(jù)結(jié)構(gòu)存在,排序的依據(jù)就是score。本文就將利用zset score這個(gè)排序的這個(gè)特性,來實(shí)現(xiàn)延時(shí)任務(wù),感興趣的可以了解一下2022-08-08spring boot中多線程開發(fā)的注意事項(xiàng)總結(jié)
spring boot 通過任務(wù)執(zhí)行器 taskexecutor 來實(shí)現(xiàn)多線程和并發(fā)編程。下面這篇文章主要給大家介紹了關(guān)于spring boot中多線程開發(fā)的注意事項(xiàng),文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-09-09最新評論