Java字节序转换

  1. 转载:http://blog.csdn.net/antswallow/article/details/5477047
  2. /**  
  3. * 通信格式转换  
  4. *  
  5. * Java 和一些windows编程语言如c、c++、delphi所写的网络程序进行通讯时,需要进行相应的转换  
  6. * 高、低字节之间的转换  
  7. * windows的字节序为低字节开头  
  8. * linux,unix的字节序为高字节开头  
  9. * java则无论平台变化,都是高字节开头  
  10.   */    
  11.   
  12. public   class  FormatTransfer {  
  13. /**  
  14.   * 将 int转为低字节在前,高字节在后的byte数组  
  15.   * @param n int  
  16.   * @return byte[]  
  17.   */   
  18. public   static   byte [] toLH( int  n) {  
  19.   byte [] b =  new   byte [ 4 ];  
  20.   b[0 ] = ( byte ) (n &  0xff );  
  21.   b[1 ] = ( byte ) (n >>  8  &  0xff );  
  22.   b[2 ] = ( byte ) (n >>  16  &  0xff );  
  23.   b[3 ] = ( byte ) (n >>  24  &  0xff );  
  24.   return  b;  
  25. }   
  26.   
  27. /**  
  28.   * 将 int转为高字节在前,低字节在后的byte数组  
  29.   * @param n int  
  30.   * @return byte[]  
  31.   */   
  32. public   static   byte [] toHH( int  n) {  
  33.   byte [] b =  new   byte [ 4 ];  
  34.   b[3 ] = ( byte ) (n &  0xff );  
  35.   b[2 ] = ( byte ) (n >>  8  &  0xff );  
  36.   b[1 ] = ( byte ) (n >>  16  &  0xff );  
  37.   b[0 ] = ( byte ) (n >>  24  &  0xff );  
  38.   return  b;  
  39. }   
  40.   
  41. /**  
  42.   * 将 short转为低字节在前,高字节在后的byte数组  
  43.   * @param n short  
  44.   * @return byte[]  
  45.   */   
  46. public   static   byte [] toLH( short  n) {  
  47.   byte [] b =  new   byte [ 2 ];  
  48.   b[0 ] = ( byte ) (n &  0xff );  
  49.   b[1 ] = ( byte ) (n >>  8  &  0xff );  
  50.   return  b;  
  51. }   
  52.   
  53. /**  
  54.   * 将 short转为高字节在前,低字节在后的byte数组  
  55.   * @param n short  
  56.   * @return byte[]  
  57.   */   
  58. public   static   byte [] toHH( short  n) {  
  59.   byte [] b =  new   byte [ 2 ];  
  60.   b[1 ] = ( byte ) (n &  0xff );  
  61.   b[0 ] = ( byte ) (n >>  8  &  0xff );  
  62.   return  b;  
  63. }   
  64.   
  65.   
  66.   
  67. /**  
  68.   * 将 将int转为高字节在前,低字节在后的byte数组   
  69.  
  70. public static byte[] toHH(int number) {  
  71.   int temp = number;  
  72.   byte[] b = new byte[4];  
  73.   for (int i = b.length - 1; i > -1; i--) {  
  74.     b = new Integer(temp & 0xff).byteValue();  
  75.     temp = temp >> 8;  
  76.   }  
  77.   return b;  
  78.   
  79.  
  80. public static byte[] IntToByteArray(int i) {  
  81.     byte[] abyte0 = new byte[4];  
  82.     abyte0[3] = (byte) (0xff & i);  
  83.     abyte0[2] = (byte) ((0xff00 & i) >> 8);  
  84.     abyte0[1] = (byte) ((0xff0000 & i) >> 16);  
  85.     abyte0[0] = (byte) ((0xff000000 & i) >> 24);  
  86.     return abyte0;  
  87.   
  88.  
  89.  
  90. */    
  91.   
  92. /**  
  93.   * 将 float转为低字节在前,高字节在后的byte数组  
  94.   */   
  95. public   static   byte [] toLH( float  f) {  
  96.   return  toLH(Float.floatToRawIntBits(f));  
  97. }   
  98.   
  99. /**  
  100.   * 将 float转为高字节在前,低字节在后的byte数组  
  101.   */   
  102. public   static   byte [] toHH( float  f) {  
  103.   return  toHH(Float.floatToRawIntBits(f));  
  104. }   
  105.   
  106. /**  
  107.   * 将 String转为byte数组  
  108.   */   
  109. public   static   byte [] stringToBytes(String s,  int  length) {  
  110.   while  (s.getBytes().length < length) {  
  111.     s += " " ;  
  112.   }  
  113.   return  s.getBytes();  
  114. }   
  115.   
  116.   
  117. /**  
  118.   * 将 字节数组转换为String  
  119.   * @param b byte[]  
  120.   * @return String  
  121.   */   
  122. public   static  String bytesToString( byte [] b) {  
  123.   StringBuffer result = new  StringBuffer( "" );  
  124.   int  length = b.length;  
  125.   for  ( int  i= 0 ; i<length; i++) {  
  126.     result.append((char )(b &  0xff ));  
  127.   }  
  128.   return  result.toString();  
  129. }   
  130.   
  131. /**  
  132.   * 将 字符串转换为byte数组  
  133.   * @param s String  
  134.   * @return byte[]  
  135.   */   
  136. public   static   byte [] stringToBytes(String s) {  
  137.   return  s.getBytes();  
  138. }   
  139.   
  140. /**  
  141.   * 将 高字节数组转换为int  
  142.   * @param b byte[]  
  143.   * @return int  
  144.   */   
  145. public   static   int  hBytesToInt( byte [] b) {  
  146.   int  s =  0 ;  
  147.   for  ( int  i =  0 ; i <  3 ; i++) {  
  148.     if  (b >=  0 ) {  
  149.     s = s + b;  
  150.     } else  {  
  151.     s = s + 256  + b;  
  152.     }  
  153.     s = s * 256 ;  
  154.   }  
  155.   if  (b[ 3 ] >=  0 ) {  
  156.     s = s + b[3 ];  
  157.   } else  {  
  158.     s = s + 256  + b[ 3 ];  
  159.   }  
  160.   return  s;  
  161. }   
  162.   
  163. /**  
  164.   * 将 低字节数组转换为int  
  165.   * @param b byte[]  
  166.   * @return int  
  167.   */   
  168. public   static   int  lBytesToInt( byte [] b) {  
  169.   int  s =  0 ;  
  170.   for  ( int  i =  0 ; i <  3 ; i++) {  
  171.     if  (b[ 3 -i] >=  0 ) {  
  172.     s = s + b[3 -i];  
  173.     } else  {  
  174.     s = s + 256  + b[ 3 -i];  
  175.     }  
  176.     s = s * 256 ;  
  177.   }  
  178.   if  (b[ 0 ] >=  0 ) {  
  179.     s = s + b[0 ];  
  180.   } else  {  
  181.     s = s + 256  + b[ 0 ];  
  182.   }  
  183.   return  s;  
  184. }   
  185.   
  186.   
  187. /**  
  188.   * 高 字节数组到short的转换  
  189.   * @param b byte[]  
  190.   * @return short  
  191.   */   
  192. public   static   short  hBytesToShort( byte [] b) {  
  193.   int  s =  0 ;  
  194.   if  (b[ 0 ] >=  0 ) {  
  195.     s = s + b[0 ];  
  196.     } else  {  
  197.     s = s + 256  + b[ 0 ];  
  198.     }  
  199.     s = s * 256 ;  
  200.   if  (b[ 1 ] >=  0 ) {  
  201.     s = s + b[1 ];  
  202.   } else  {  
  203.     s = s + 256  + b[ 1 ];  
  204.   }  
  205.   short  result = ( short )s;  
  206.   return  result;  
  207. }   
  208.   
  209. /**  
  210.   * 低 字节数组到short的转换  
  211.   * @param b byte[]  
  212.   * @return short  
  213.   */   
  214. public   static   short  lBytesToShort( byte [] b) {  
  215.   int  s =  0 ;  
  216.   if  (b[ 1 ] >=  0 ) {  
  217.     s = s + b[1 ];  
  218.     } else  {  
  219.     s = s + 256  + b[ 1 ];  
  220.     }  
  221.     s = s * 256 ;  
  222.   if  (b[ 0 ] >=  0 ) {  
  223.     s = s + b[0 ];  
  224.   } else  {  
  225.     s = s + 256  + b[ 0 ];  
  226.   }  
  227.   short  result = ( short )s;  
  228.   return  result;  
  229. }   
  230.   
  231. /**  
  232.   * 高 字节数组转换为float  
  233.   * @param b byte[]  
  234.   * @return float  
  235.   */   
  236. public   static   float  hBytesToFloat( byte [] b) {  
  237.   int  i =  0 ;  
  238.   Float F = new  Float( 0.0 );  
  239.   i = ((((b[0 ]& 0xff )<< 8  | (b[ 1 ]& 0xff ))<< 8 ) | (b[ 2 ]& 0xff ))<< 8  | (b[ 3 ]& 0xff );  
  240.   return  F.intBitsToFloat(i);  
  241. }   
  242.   
  243. /**  
  244.   * 低 字节数组转换为float  
  245.   * @param b byte[]  
  246.   * @return float  
  247.   */   
  248. public   static   float  lBytesToFloat( byte [] b) {  
  249.   int  i =  0 ;  
  250.   Float F = new  Float( 0.0 );  
  251.   i = ((((b[3 ]& 0xff )<< 8  | (b[ 2 ]& 0xff ))<< 8 ) | (b[ 1 ]& 0xff ))<< 8  | (b[ 0 ]& 0xff );  
  252.   return  F.intBitsToFloat(i);  
  253. }   
  254.   
  255. /**  
  256.   * 将 byte数组中的元素倒序排列  
  257.   */   
  258. public   static   byte [] bytesReverseOrder( byte [] b) {  
  259.   int  length = b.length;  
  260.   byte [] result =  new   byte [length];  
  261.   for ( int  i= 0 ; i<length; i++) {  
  262.     result[length-i-1 ] = b;  
  263.   }  
  264.   return  result;  
  265. }   
  266.   
  267. /**  
  268.   * 打 印byte数组  
  269.   */   
  270. public   static   void  printBytes( byte [] bb) {  
  271.   int  length = bb.length;  
  272.   for  ( int  i= 0 ; i<length; i++) {  
  273.     System.out.print(bb + " " );  
  274.   }  
  275.   System.out.println("" );  
  276. }   
  277.   
  278. public   static   void  logBytes( byte [] bb) {  
  279.   int  length = bb.length;  
  280.   String out = "" ;  
  281.   for  ( int  i= 0 ; i<length; i++) {  
  282.     out = out + bb + " " ;  
  283.   }   
  284.   
  285. }   
  286.   
  287. /**  
  288.   * 将 int类型的值转换为字节序颠倒过来对应的int值  
  289.   * @param i int  
  290.   * @return int  
  291.   */   
  292. public   static   int  reverseInt( int  i) {  
  293.   int  result = FormatTransfer.hBytesToInt(FormatTransfer.toLH(i));  
  294.   return  result;  
  295. }   
  296.   
  297. /**  
  298.   * 将 short类型的值转换为字节序颠倒过来对应的short值  
  299.   * @param s short  
  300.   * @return short  
  301.   */   
  302. public   static   short  reverseShort( short  s) {  
  303.   short  result = FormatTransfer.hBytesToShort(FormatTransfer.toLH(s));  
  304.   return  result;  
  305. }   
  306.   
  307. /**  
  308.   * 将 float类型的值转换为字节序颠倒过来对应的float值  
  309.   * @param f float  
  310.   * @return float  
  311.   */   
  312. public   static   float  reverseFloat( float  f) {  
  313.   float  result = FormatTransfer.hBytesToFloat(FormatTransfer.toLH(f));  
  314.   return  result;  
  315. }   
  316.   
  317. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值