Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 209|回复: 0

[JavaIO学习]Java字节顺序转换

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-28 23:56:19 | 显示全部楼层 |阅读模式
    1. /**  
    2. * 通信格式转换  
    3. *  
    4. * Java 和一些windows编程语言如c、c++、delphi所写的网络程序进行通讯时,需要进行相应的转换  
    5. * 高、低字节之间的转换  
    6. * windows的字节序为低字节开头  
    7. * linux,unix的字节序为高字节开头  
    8. * java则无论平台变化,都是高字节开头  
    9.    */   
    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. /**  
    29.    * 将 int转为高字节在前,低字节在后的byte数组  
    30.    * @param n int  
    31.    * @return byte[]  
    32.    */   
    33. public   static   byte [] toHH( int  n) {  
    34.    byte [] b =  new   byte [ 4 ];  
    35.    b[3 ] = ( byte ) (n &  0xff );  
    36.    b[2 ] = ( byte ) (n >>  8  &  0xff );  
    37.    b[1 ] = ( byte ) (n >>  16  &  0xff );  
    38.    b[0 ] = ( byte ) (n >>  24  &  0xff );  
    39.    return  b;  
    40. }   
    41.    
    42. /**  
    43.    * 将 short转为低字节在前,高字节在后的byte数组  
    44.    * @param n short  
    45.    * @return byte[]  
    46.    */   
    47. public   static   byte [] toLH( short  n) {  
    48.    byte [] b =  new   byte [ 2 ];  
    49.    b[0 ] = ( byte ) (n &  0xff );  
    50.    b[1 ] = ( byte ) (n >>  8  &  0xff );  
    51.    return  b;  
    52. }

    53.    
    54. /**  
    55.    * 将 short转为高字节在前,低字节在后的byte数组  
    56.    * @param n short  
    57.    * @return byte[]  
    58.    */   
    59. public   static   byte [] toHH( short  n) {  
    60.    byte [] b =  new   byte [ 2 ];  
    61.    b[1 ] = ( byte ) (n &  0xff );  
    62.    b[0 ] = ( byte ) (n >>  8  &  0xff );  
    63.    return  b;  
    64. }   
    65.    
    66.    
    67.    
    68. /**  
    69.    * 将将int转为高字节在前,低字节在后的byte数组   
    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. 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.    * 将 float转为低字节在前,高字节在后的byte数组  
    91.    */   
    92. public   static   byte [] toLH( float  f) {  
    93.    return  toLH(Float.floatToRawIntBits(f));  
    94. }   
    95. /**  
    96.    * 将 float转为高字节在前,低字节在后的byte数组  
    97.    */   
    98. public   static   byte [] toHH( float  f) {  
    99.    return  toHH(Float.floatToRawIntBits(f));  
    100. }

    101.    
    102. /**  
    103.    * 将 String转为byte数组  
    104.    */   
    105. public   static   byte [] stringToBytes(String s,  int  length) {  
    106.    while  (s.getBytes().length < length) {  
    107.      s += " " ;  
    108.    }  
    109.    return  s.getBytes();  
    110. }   
    111.    
    112.    
    113. /**  
    114.    * 将字节数组转换为String  
    115.    * @param b byte[]  
    116.    * @return String  
    117.    */   
    118. public   static  String bytesToString( byte [] b) {  
    119.    StringBuffer result = new  StringBuffer( "" );  
    120.    int  length = b.length;  
    121.    for  ( int  i= 0 ; i< length; i++) {  
    122.      result.append((char )(b[i] &  0xff ));  
    123.    }  
    124.    return  result.toString();  
    125. }

    126.    
    127. /**  
    128.    * 将字符串转换为byte数组  
    129.    * @param s String  
    130.    * @return byte[]  
    131.    */   
    132. public   static   byte [] stringToBytes(String s) {  
    133.    return  s.getBytes();  
    134. }

    135.    
    136. /**  
    137.    * 将高字节数组转换为int  
    138.    * @param b byte[]  
    139.    * @return int  
    140.    */   
    141. public   static   int  hBytesToInt( byte [] b) {  
    142.    int  s =  0 ;  
    143.    for  ( int  i =  0 ; i <  3 ; i++) {  
    144.      if  (b[i] >=  0 ) {  
    145.      s = s + b[i];  
    146.      } else  {  
    147.      s = s + 256  + b[i];  
    148.      }  
    149.      s = s * 256 ;  
    150.    }  
    151.    if  (b[ 3 ] >=  0 ) {  
    152.      s = s + b[3 ];  
    153.    } else  {  
    154.      s = s + 256  + b[ 3 ];  
    155.    }  
    156.    return  s;  
    157. }

    158.    
    159. /**  
    160.    * 将低字节数组转换为int  
    161.    * @param b byte[]  
    162.    * @return int  
    163.    */   
    164. public   static   int  lBytesToInt( byte [] b) {  
    165.    int  s =  0 ;  
    166.    for  ( int  i =  0 ; i <  3 ; i++) {  
    167.      if  (b[ 3 -i] >=  0 ) {  
    168.      s = s + b[3 -i];  
    169.      } else  {  
    170.      s = s + 256  + b[ 3 -i];  
    171.      }  
    172.      s = s * 256 ;  
    173.    }  
    174.    if  (b[ 0 ] >=  0 ) {  
    175.      s = s + b[0 ];  
    176.    } else  {  
    177.      s = s + 256  + b[ 0 ];  
    178.    }  
    179.    return  s;  
    180. }   
    181.    
    182.    
    183. /**  
    184.    * 高字节数组到short的转换  
    185.    * @param b byte[]  
    186.    * @return short  
    187.    */   
    188. public   static   short  hBytesToShort( byte [] b) {  
    189.    int  s =  0 ;  
    190.    if  (b[ 0 ] >=  0 ) {  
    191.      s = s + b[0 ];  
    192.      } else  {  
    193.      s = s + 256  + b[ 0 ];  
    194.      }  
    195.      s = s * 256 ;  
    196.    if  (b[ 1 ] >=  0 ) {  
    197.      s = s + b[1 ];  
    198.    } else  {  
    199.      s = s + 256  + b[ 1 ];  
    200.    }  
    201.    short  result = ( short )s;  
    202.    return  result;  
    203. }

    204.    
    205. /**  
    206.    * 低字节数组到short的转换  
    207.    * @param b byte[]  
    208.    * @return short  
    209.    */   
    210. public   static   short  lBytesToShort( byte [] b) {  
    211.    int  s =  0 ;  
    212.    if  (b[ 1 ] >=  0 ) {  
    213.      s = s + b[1 ];  
    214.      } else  {  
    215.      s = s + 256  + b[ 1 ];  
    216.      }  
    217.      s = s * 256 ;  
    218.    if  (b[ 0 ] >=  0 ) {  
    219.      s = s + b[0 ];  
    220.    } else  {  
    221.      s = s + 256  + b[ 0 ];  
    222.    }  
    223.    short  result = ( short )s;  
    224.    return  result;  
    225. }

    226.    
    227. /**  
    228.    * 高字节数组转换为float  
    229.    * @param b byte[]  
    230.    * @return float  
    231.    */   
    232. public   static   float  hBytesToFloat( byte [] b) {  
    233.    int  i =  0 ;  
    234.    Float F = new  Float( 0.0 );  
    235.    i = ((((b[0 ]& 0xff )<< 8  | (b[ 1 ]& 0xff ))<< 8 ) | (b[ 2 ]& 0xff ))<< 8  | (b[ 3 ]& 0xff );  
    236.    return  F.intBitsToFloat(i);  
    237. }

    238.    
    239. /**  
    240.    * 低字节数组转换为float  
    241.    * @param b byte[]  
    242.    * @return float  
    243.    */   
    244. public   static   float  lBytesToFloat( byte [] b) {  
    245.    int  i =  0 ;  
    246.    Float F = new  Float( 0.0 );  
    247.    i = ((((b[3 ]& 0xff )<< 8  | (b[ 2 ]& 0xff ))<< 8 ) | (b[ 1 ]& 0xff ))<< 8  | (b[ 0 ]& 0xff );  
    248.    return  F.intBitsToFloat(i);  
    249. }

    250.    
    251. /**  
    252.    * 将 byte数组中的元素倒序排列  
    253.    */   
    254. public   static   byte [] bytesReverseOrder( byte [] b) {  
    255.    int  length = b.length;  
    256.    byte [] result =  new   byte [length];  
    257.    for ( int  i= 0 ; i< length; i++) {  
    258.      result[length-i-1 ] = b[i];  
    259.    }  
    260.    return  result;  
    261. }

    262.    
    263. /**  
    264.    * 打印byte数组  
    265.    */   
    266. public   static   void  printBytes( byte [] bb) {  
    267.    int  length = bb.length;  
    268.    for  ( int  i= 0 ; i< length; i++) {  
    269.      System.out.print(bb + " " );  
    270.    }  
    271.    System.out.println("" );  
    272. }   
    273.    
    274. public   static   void  logBytes( byte [] bb) {  
    275.    int  length = bb.length;  
    276.    String out = "" ;  
    277.    for  ( int  i= 0 ; i< length; i++) {  
    278.      out = out + bb + " " ;  
    279.    }   
    280.    
    281. }   
    282.    
    283. /**  
    284.    * 将 int类型的值转换为字节序颠倒过来对应的int值  
    285.    * @param i int  
    286.    * @return int  
    287.    */   
    288. public   static   int  reverseInt( int  i) {  
    289.    int  result = FormatTransfer.hBytesToInt(FormatTransfer.toLH(i));  
    290.    return  result;  
    291. }   
    292.    
    293. /**  
    294.    * 将 short类型的值转换为字节序颠倒过来对应的short值  
    295.    * @param s short  
    296.    * @return short  
    297.    */   
    298. public   static   short  reverseShort( short  s) {  
    299.    short  result = FormatTransfer.hBytesToShort(FormatTransfer.toLH(s));  
    300.    return  result;  
    301. }   
    302.    
    303. /**  
    304.    * 将 float类型的值转换为字节序颠倒过来对应的float值  
    305.    * @param f float  
    306.    * @return float  
    307.    */   
    308. public   static   float  reverseFloat( float  f) {  
    309.    float  result = FormatTransfer.hBytesToFloat(FormatTransfer.toLH(f));  
    310.    return  result;  
    311. }   
    312.    
    313. }  
    复制代码


      
      
       
       

         
       

         
       
      
    复制代码

    源码下载:http://file.javaxxz.com/2014/10/28/235619500.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 02:05 , Processed in 0.335903 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表