现在的位置: 首页 > 综合 > 正文

Java字节序转换

2013年08月10日 ⁄ 综合 ⁄ 共 5894字 ⁄ 字号 评论关闭
  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.   

抱歉!评论已关闭.