关于字节序的讨论

字节序,顾名思义就是字节存放的顺序


字节序分为两种:

BIG-ENDIAN----大字节序

LITTLE-ENDIAN----小字节序


BIG-ENDIAN、LITTLE-ENDIAN与多字节类型的数据有关的比如int,short,long型,而对单字节数据byte却没有影响。

BIG-ENDIAN就是最低地址存放最高有效字节。

LITTLE-ENDIAN是最低地址存放最低有效字节。即常说的低位在先,高位在后。 


Java中int类型占4个字节,一定要是“多字节类型的数据”才有字节序问题,汉字编码也有这个问题。请看下面4字节的例子:


  比如 int a = 0x05060708 


  在BIG-ENDIAN的情况下存放为: 

  低地址------->高地址 

  字节号: 第0字节,第1字节,第2字节,第3字节 

  数  据: 05    , 06   , 07   , 08 


  在LITTLE-ENDIAN的情况下存放为: 

  低地址------->高地址 

  字节号: 第0字节,第1字节,第2字节,第3字节 

  数  据: 08    , 07   , 06   , 05 



JAVA字节序:

指的是在JAVA虚拟机中多字节类型数据的存放顺序,JAVA字节序也是BIG-ENDIAN。 


主机字节序:

Intel的x86系列CPU是Little-Endian,而PowerPC 、SPARC和Motorola处理器是BIG-ENDIAN。

ARM同时支持 big和little,实际应用中通常使用little endian。是BIG-ENDIAN还是LITTLE-ENDIAN的跟CPU有关的,每一种CPU不是BIG-ENDIAN就是LITTLE-ENDIAN。


网络字节序:

4个字节的32 bit值以下面的次序传输:首先是0~7bit,其次8~15bit,然后16~23bit,最后是24~31bit。这种传输次序称作大端字节序(BIG-ENDIAN)。 TCP/IP首部中所有的二进制整数在网络中传输时都要求以这种次序。


不同的CPU上运行不同的操作系统,字节序也是不同的,参见下表。

    处理器     操作系统   字节排序

    Alpha     全部        Little endian

    HP-PA     NT          Little endian

    HP-PA     UNIX        Big endian

    Intelx86  全部        Little endian



所以在用C/C++写通信程序时,在发送数据前务必用htonl和htons去把整型和短整型的数据进行从主机字节序到网络字节序的转换,而接收数据后对于整型和短整型数据则必须调用ntohl和ntohs实现从网络字节序到主机字节序的转换。如果通信的一方是JAVA程序、一方是C/C++程序时,则需要在C/C++一侧使用以上几个方法进行字节序的转换,而JAVA一侧,则不需要做任何处理,因为JAVA字节序与网络字节序都是BIG-ENDIAN,只要C/C++一侧能正确进行转换即可(发送前从主机序到网络序,接收时反变换)。如果通信的双方都是JAVA,则根本不用考虑字节序的问题了。 

 

java中转换字节序

 

方案一:通过ByteBuffer实现

 

 

方案二:自己写代码实现

 

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

ByteBuffer类中的order(ByteOrder bo) 方法可以设置 ByteBuffer 的字节序。 

 

其中的ByteOrder是枚举: 

ByteOrder BIG_ENDIAN  代表大字节序的 ByteOrder 。

ByteOrder LITTLE_ENDIAN  代表小字节序的 ByteOrder 。

ByteOrder nativeOrder()  返回当前硬件平台的字节序。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值