JAVA 字节顺序转换

来源:互联网 发布:vs2017如何编写c语言 编辑:程序博客网 时间:2024/05/22 10:54
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. }  




扩展:
  1. /** 
  2.   * 将double转为低字节在前,高字节在后的byte数组 
  3.   * @param n double 
  4.   * @return byte[] 
  5.   */  
  1. public static byte[] toLH(double n) { 
    long raw = Double.doubleToRawLongBits(n);
    int h = (int)(raw >> 32);
    int l = (int)(raw & 0x00000000FFFFFFFF);

    byte[] hb = toLH(h);
    byte[] lb = toLH(l);

    byte[] res = new byte[8];
    System.arraycopy(hb, 0, res, 4, 4);
    System.arraycopy(lb, 0, res, 0, 4);

    return res;
byte[]数组转为Double
  1. static double ArryToDouble(byte[] Array,int Pos)     
    {   
        long accum = 0;   
        accum = Array[Pos+0] & 0xFF;  
        accum |= (long)(Array[Pos+1] & 0xFF)<<8;  
        accum |= (long)(Array[Pos+2] & 0xFF)<<16;  
        accum |= (long)(Array[Pos+3] & 0xFF)<<24;  
        accum |= (long)(Array[Pos+4] & 0xFF)<<32;  
        accum |= (long)(Array[Pos+5] & 0xFF)<<40;  
        accum |= (long)(Array[Pos+6] & 0xFF)<<48;  
        accum |= (long)(Array[Pos+7] & 0xFF)<<56;  
        return Double.longBitsToDouble(accum);   

    }  
    byte[]转为float
    static float ArryToFloat(byte[] Array,int Pos)     
    {   
        int accum = 0;   
        accum = Array[Pos+0] & 0xFF;  
        accum |= (long)(Array[Pos+1] & 0xFF)<<8;  
        accum |= (long)(Array[Pos+2] & 0xFF)<<16;  
        accum |= (long)(Array[Pos+3] & 0xFF)<<24;  
        return Float.intBitsToFloat(accum);   

    public static int bytesToInt(byte[] src, int offset) {  
       int value;    
       value = (int) ((src[offset] & 0xFF)   
               | ((src[offset+1] & 0xFF)<<8)   
               | ((src[offset+2] & 0xFF)<<16)   
               | ((src[offset+3] & 0xFF)<<24));  
       return value;  
    }  
    float转换为byte[]
    static byte[] FloatToArray(float Value)  
    {  
        int accum = Float.floatToRawIntBits(Value);  
        byte[] byteRet = new byte[4];  
        byteRet[0] = (byte)(accum & 0xFF);  
        byteRet[1] = (byte)((accum>>8) & 0xFF);  
        byteRet[2] = (byte)((accum>>16) & 0xFF);  
        byteRet[3] = (byte)((accum>>24) & 0xFF);  
        return byteRet;  
    }  

转自:http://origin100.iteye.com/blog/267165
0 0