Java字節序轉換

  1. /**  

  2. * 通信格式轉換  

  3. *  

  4. * Java 和一些windows編程語言如c、c++、delphi所寫的網絡程序進行通訊時,需要進行相應的轉換  

  5. * 高、低字節之間的轉換  

  6. * windows的字節序爲低字節開頭  

  7. * linux,unix的字節序爲高字節開頭  

  8. * java則無論平臺變化,都是高字節開頭  

  9.   */    

  10.   

  11. public   class  FormatTransfer {  

  12. /**  

  13.   * 將 int轉爲低字節在前,高字節在後的byte數組  

  14.   * @param n int  

  15.   * @return byte[]  

  16.   */   

  17. public   static   byte [] toLH( int  n) {  

  18.   byte [] b =  new   byte [ 4 ];  

  19.   b[0 ] = ( byte ) (n &  0xff );  

  20.   b[1 ] = ( byte ) (n >>  8  &  0xff );  

  21.   b[2 ] = ( byte ) (n >>  16  &  0xff );  

  22.   b[3 ] = ( byte ) (n >>  24  &  0xff );  

  23.   return  b;  

  24. }   

  25.   

  26. /**  

  27.   * 將 int轉爲高字節在前,低字節在後的byte數組  

  28.   * @param n int  

  29.   * @return byte[]  

  30.   */   

  31. public   static   byte [] toHH( int  n) {  

  32.   byte [] b =  new   byte [ 4 ];  

  33.   b[3 ] = ( byte ) (n &  0xff );  

  34.   b[2 ] = ( byte ) (n >>  8  &  0xff );  

  35.   b[1 ] = ( byte ) (n >>  16  &  0xff );  

  36.   b[0 ] = ( byte ) (n >>  24  &  0xff );  

  37.   return  b;  

  38. }   

  39.   

  40. /**  

  41.   * 將 short轉爲低字節在前,高字節在後的byte數組  

  42.   * @param n short  

  43.   * @return byte[]  

  44.   */   

  45. public   static   byte [] toLH( short  n) {  

  46.   byte [] b =  new   byte [ 2 ];  

  47.   b[0 ] = ( byte ) (n &  0xff );  

  48.   b[1 ] = ( byte ) (n >>  8  &  0xff );  

  49.   return  b;  

  50. }   

  51.   

  52. /**  

  53.   * 將 short轉爲高字節在前,低字節在後的byte數組  

  54.   * @param n short  

  55.   * @return byte[]  

  56.   */   

  57. public   static   byte [] toHH( short  n) {  

  58.   byte [] b =  new   byte [ 2 ];  

  59.   b[1 ] = ( byte ) (n &  0xff );  

  60.   b[0 ] = ( byte ) (n >>  8  &  0xff );  

  61.   return  b;  

  62. }   

  63.   

  64.   

  65.   

  66. /**  

  67.   * 將 將int轉爲高字節在前,低字節在後的byte數組   

  68.  

  69. public static byte[] toHH(int number) {  

  70.   int temp = number;  

  71.   byte[] b = new byte[4];  

  72.   for (int i = b.length - 1; i > -1; i--) {  

  73.     b = new Integer(temp & 0xff).byteValue();  

  74.     temp = temp >> 8;  

  75.   }  

  76.   return b;  

  77.   

  78.  

  79. public static byte[] IntToByteArray(int i) {  

  80.     byte[] abyte0 = new byte[4];  

  81.     abyte0[3] = (byte) (0xff & i);  

  82.     abyte0[2] = (byte) ((0xff00 & i) >> 8);  

  83.     abyte0[1] = (byte) ((0xff0000 & i) >> 16);  

  84.     abyte0[0] = (byte) ((0xff000000 & i) >> 24);  

  85.     return abyte0;  

  86.   

  87.  

  88.  

  89. */    

  90.   

  91. /**  

  92.   * 將 float轉爲低字節在前,高字節在後的byte數組  

  93.   */   

  94. public   static   byte [] toLH( float  f) {  

  95.   return  toLH(Float.floatToRawIntBits(f));  

  96. }   

  97.   

  98. /**  

  99.   * 將 float轉爲高字節在前,低字節在後的byte數組  

  100.   */   

  101. public   static   byte [] toHH( float  f) {  

  102.   return  toHH(Float.floatToRawIntBits(f));  

  103. }   

  104.   

  105. /**  

  106.   * 將 String轉爲byte數組  

  107.   */   

  108. public   static   byte [] stringToBytes(String s,  int  length) {  

  109.   while  (s.getBytes().length < length) {  

  110.     s += " " ;  

  111.   }  

  112.   return  s.getBytes();  

  113. }   

  114.   

  115.   

  116. /**  

  117.   * 將 字節數組轉換爲String  

  118.   * @param b byte[]  

  119.   * @return String  

  120.   */   

  121. public   static  String bytesToString( byte [] b) {  

  122.   StringBuffer result = new  StringBuffer( "" );  

  123.   int  length = b.length;  

  124.   for  ( int  i= 0 ; i<length; i++) {  

  125.     result.append((char )(b &  0xff ));  

  126.   }  

  127.   return  result.toString();  

  128. }   

  129.   

  130. /**  

  131.   * 將 字符串轉換爲byte數組  

  132.   * @param s String  

  133.   * @return byte[]  

  134.   */   

  135. public   static   byte [] stringToBytes(String s) {  

  136.   return  s.getBytes();  

  137. }   

  138.   

  139. /**  

  140.   * 將 高字節數組轉換爲int  

  141.   * @param b byte[]  

  142.   * @return int  

  143.   */   

  144. public   static   int  hBytesToInt( byte [] b) {  

  145.   int  s =  0 ;  

  146.   for  ( int  i =  0 ; i <  3 ; i++) {  

  147.     if  (b >=  0 ) {  

  148.     s = s + b;  

  149.     } else  {  

  150.     s = s + 256  + b;  

  151.     }  

  152.     s = s * 256 ;  

  153.   }  

  154.   if  (b[ 3 ] >=  0 ) {  

  155.     s = s + b[3 ];  

  156.   } else  {  

  157.     s = s + 256  + b[ 3 ];  

  158.   }  

  159.   return  s;  

  160. }   

  161.   

  162. /**  

  163.   * 將 低字節數組轉換爲int  

  164.   * @param b byte[]  

  165.   * @return int  

  166.   */   

  167. public   static   int  lBytesToInt( byte [] b) {  

  168.   int  s =  0 ;  

  169.   for  ( int  i =  0 ; i <  3 ; i++) {  

  170.     if  (b[ 3 -i] >=  0 ) {  

  171.     s = s + b[3 -i];  

  172.     } else  {  

  173.     s = s + 256  + b[ 3 -i];  

  174.     }  

  175.     s = s * 256 ;  

  176.   }  

  177.   if  (b[ 0 ] >=  0 ) {  

  178.     s = s + b[0 ];  

  179.   } else  {  

  180.     s = s + 256  + b[ 0 ];  

  181.   }  

  182.   return  s;  

  183. }   

  184.   

  185.   

  186. /**  

  187.   * 高 字節數組到short的轉換  

  188.   * @param b byte[]  

  189.   * @return short  

  190.   */   

  191. public   static   short  hBytesToShort( byte [] b) {  

  192.   int  s =  0 ;  

  193.   if  (b[ 0 ] >=  0 ) {  

  194.     s = s + b[0 ];  

  195.     } else  {  

  196.     s = s + 256  + b[ 0 ];  

  197.     }  

  198.     s = s * 256 ;  

  199.   if  (b[ 1 ] >=  0 ) {  

  200.     s = s + b[1 ];  

  201.   } else  {  

  202.     s = s + 256  + b[ 1 ];  

  203.   }  

  204.   short  result = ( short )s;  

  205.   return  result;  

  206. }   

  207.   

  208. /**  

  209.   * 低 字節數組到short的轉換  

  210.   * @param b byte[]  

  211.   * @return short  

  212.   */   

  213. public   static   short  lBytesToShort( byte [] b) {  

  214.   int  s =  0 ;  

  215.   if  (b[ 1 ] >=  0 ) {  

  216.     s = s + b[1 ];  

  217.     } else  {  

  218.     s = s + 256  + b[ 1 ];  

  219.     }  

  220.     s = s * 256 ;  

  221.   if  (b[ 0 ] >=  0 ) {  

  222.     s = s + b[0 ];  

  223.   } else  {  

  224.     s = s + 256  + b[ 0 ];  

  225.   }  

  226.   short  result = ( short )s;  

  227.   return  result;  

  228. }   

  229.   

  230. /**  

  231.   * 高 字節數組轉換爲float  

  232.   * @param b byte[]  

  233.   * @return float  

  234.   */   

  235. public   static   float  hBytesToFloat( byte [] b) {  

  236.   int  i =  0 ;  

  237.   Float F = new  Float( 0.0 );  

  238.   i = ((((b[0 ]& 0xff )<< 8  | (b[ 1 ]& 0xff ))<< 8 ) | (b[ 2 ]& 0xff ))<< 8  | (b[ 3 ]& 0xff );  

  239.   return  F.intBitsToFloat(i);  

  240. }   

  241.   

  242. /**  

  243.   * 低 字節數組轉換爲float  

  244.   * @param b byte[]  

  245.   * @return float  

  246.   */   

  247. public   static   float  lBytesToFloat( byte [] b) {  

  248.   int  i =  0 ;  

  249.   Float F = new  Float( 0.0 );  

  250.   i = ((((b[3 ]& 0xff )<< 8  | (b[ 2 ]& 0xff ))<< 8 ) | (b[ 1 ]& 0xff ))<< 8  | (b[ 0 ]& 0xff );  

  251.   return  F.intBitsToFloat(i);  

  252. }   

  253.   

  254. /**  

  255.   * 將 byte數組中的元素倒序排列  

  256.   */   

  257. public   static   byte [] bytesReverseOrder( byte [] b) {  

  258.   int  length = b.length;  

  259.   byte [] result =  new   byte [length];  

  260.   for ( int  i= 0 ; i<length; i++) {  

  261.     result[length-i-1 ] = b;  

  262.   }  

  263.   return  result;  

  264. }   

  265.   

  266. /**  

  267.   * 打 印byte數組  

  268.   */   

  269. public   static   void  printBytes( byte [] bb) {  

  270.   int  length = bb.length;  

  271.   for  ( int  i= 0 ; i<length; i++) {  

  272.     System.out.print(bb + " " );  

  273.   }  

  274.   System.out.println("" );  

  275. }   

  276.   

  277. public   static   void  logBytes( byte [] bb) {  

  278.   int  length = bb.length;  

  279.   String out = "" ;  

  280.   for  ( int  i= 0 ; i<length; i++) {  

  281.     out = out + bb + " " ;  

  282.   }   

  283.   

  284. }   

  285.   

  286. /**  

  287.   * 將 int類型的值轉換爲字節序顛倒過來對應的int值  

  288.   * @param i int  

  289.   * @return int  

  290.   */   

  291. public   static   int  reverseInt( int  i) {  

  292.   int  result = FormatTransfer.hBytesToInt(FormatTransfer.toLH(i));  

  293.   return  result;  

  294. }   

  295.   

  296. /**  

  297.   * 將 short類型的值轉換爲字節序顛倒過來對應的short值  

  298.   * @param s short  

  299.   * @return short  

  300.   */   

  301. public   static   short  reverseShort( short  s) {  

  302.   short  result = FormatTransfer.hBytesToShort(FormatTransfer.toLH(s));  

  303.   return  result;  

  304. }   

  305.   

  306. /**  

  307.   * 將 float類型的值轉換爲字節序顛倒過來對應的float值  

  308.   * @param f float  

  309.   * @return float  

  310.   */   

  311. public   static   float  reverseFloat( float  f) {  

  312.   float  result = FormatTransfer.hBytesToFloat(FormatTransfer.toLH(f));  

  313.   return  result;  

  314. }   

  315.   

  316. }  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章