字节序 整形与字节数组转换

Big Endian and Little Endian

    谈到字节序的问题,必然牵涉到两大CPU派系。那就是Motorola的PowerPC系列CPU和Intel的x86系列CPU。PowerPC系列采用big endian方式存储数据,而x86系列则采用little endian方式存储数据。那么究竟什么是big endian,什么又是little endian呢?

    其实big endian是指低地址存放最高有效字节(MSB),而little endian则是低地址存放最低有效字节(LSB),即常说的低位在先,高位在后。
    用文字说明可能比较抽象,下面用图像加以说明。比如数字0x12345678在两种不同字节序CPU中的存储顺序如下所示:

Big Endian

  低地址                           高地址
  ----------------------------------------->
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  |     12     |      34    |     56      |     78    |
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Little Endian

  低地址                           高地址
  ----------------------------------------->
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  |     78     |      56    |     34      |     12    |
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

    从上面两图可以看出,采用big endian方式存储数据是符合我们人类的思维习惯的。

    为什么要注意字节序的问题呢?你可能这么问。当然,如果你写的程序只在单机环境下面运行,并且不和别人的程序打交道,那么你完全可以忽略字节序的存在。但是,如果你的程序要跟别人的程序产生交互呢?尤其是当你把你在微机上运算的结果运用到计算机群上去的话。在这里我想说说两种语言。C/C++语言编写的程序里数据存储顺序是跟编译平台所在的CPU相关的,而JAVA编写的程序则唯一采用big endian方式来存储数据。试想,如果你用C/C++语言在x86平台下编写的程序跟别人的JAVA程序互通时会产生什么结果?就拿上面的 0x12345678来说,你的程序传递给别人的一个数据,将指向0x12345678的指针传给了JAVA程序,由于JAVA采取big endian方式存储数据,很自然的它会将你的数据翻译为0x78563412。什么?竟然变成另外一个数字了?是的,就是这种后果。因此,在你的C程序传给JAVA程序之前有必要进行字节序的转换工作。

    无独有偶,所有网络协议也都是采用big endian的方式来传输数据的。所以有时我们也会把big endian方式称之为网络字节序。当两台采用不同字节序的主机通信时,在发送数据之前都必须经过字节序的转换成为网络字节序后再进行传输。ANSI C中提供了四个转换字节序的宏。


BIG-ENDIAN(大字节序、高字节序)
LITTLE-ENDIAN(小字节序、低字节序)
主机字节序
网络字节顺序
JAVA字节序

1.BIG-ENDIAN、LITTLE-ENDIAN跟多字节类型的数据有关的比如int,short,long型,而对单字节数据byte却没有影响。BIG-ENDIAN就是低位字节排放在内存的低端,高位字节排放在内存的高端。而LITTLE-ENDIAN正好相反。
比如 int a = 0x05060708
在BIG-ENDIAN的情况下存放为:
字节号 0 1 2 3
数据 05 06 07 08
在LITTLE-ENDIAN的情况下存放为:
字节号 0 1 2 3
数据 08 07 06 05

2.BIG-ENDIAN、LITTLE-ENDIAN、跟CPU有关的,每一种CPU不是BIG-ENDIAN就是LITTLE-ENDIAN、。IA架构的CPU中是Little-Endian,而PowerPC 、SPARC和Motorola处理器。这其实就是所谓的主机字节序。而网络字节序是指数据在网络上传输时是大头还是小头的,在Internet的网络字节序是BIG-ENDIAN。所谓的JAVA字节序指的是在JAVA虚拟机中多字节类型数据的存放顺序,JAVA字节序也是BIG-ENDIAN。

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

4.如果网络上全部是PowerPC,SPARC和Motorola CPU的主机那么不会出现任何问题,但由于实际存在大量的IA架构的CPU,所以经常出现数据传输错误。

=======================================================================

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 ut = "";   
  280.   for (int i=0; i<length; i++) {   
  281.     ut = 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.   
  316. }  
/**
* 通信格式转换
*
* Java和一些windows编程语言如c、c++、delphi所写的网络程序进行通讯时,需要进行相应的转换
* 高、低字节之间的转换
* windows的字节序为低字节开头
* linux,unix的字节序为高字节开头
* java则无论平台变化,都是高字节开头
*/

public class FormatTransfer {
/**
  * 将int转为低字节在前,高字节在后的byte数组
  * @param n int
  * @return byte[]
  */
public static byte[] toLH(int n) {
  byte[] b = new byte[4];
  b[0] = (byte) (n & 0xff);
  b[1] = (byte) (n >> 8 & 0xff);
  b[2] = (byte) (n >> 16 & 0xff);
  b[3] = (byte) (n >> 24 & 0xff);
  return b;
}

/**
  * 将int转为高字节在前,低字节在后的byte数组
  * @param n int
  * @return byte[]
  */
public static byte[] toHH(int n) {
  byte[] b = new byte[4];
  b[3] = (byte) (n & 0xff);
  b[2] = (byte) (n >> 8 & 0xff);
  b[1] = (byte) (n >> 16 & 0xff);
  b[0] = (byte) (n >> 24 & 0xff);
  return b;
}

/**
  * 将short转为低字节在前,高字节在后的byte数组
  * @param n short
  * @return byte[]
  */
public static byte[] toLH(short n) {
  byte[] b = new byte[2];
  b[0] = (byte) (n & 0xff);
  b[1] = (byte) (n >> 8 & 0xff);
  return b;
}

/**
  * 将short转为高字节在前,低字节在后的byte数组
  * @param n short
  * @return byte[]
  */
public static byte[] toHH(short n) {
  byte[] b = new byte[2];
  b[1] = (byte) (n & 0xff);
  b[0] = (byte) (n >> 8 & 0xff);
  return b;
}

 

/**
  * 将将int转为高字节在前,低字节在后的byte数组

public static byte[] toHH(int number) {
  int temp = number;
  byte[] b = new byte[4];
  for (int i = b.length - 1; i > -1; i--) {
    b = new Integer(temp & 0xff).byteValue();
    temp = temp >> 8;
  }
  return b;
}

public static byte[] IntToByteArray(int i) {
    byte[] abyte0 = new byte[4];
    abyte0[3] = (byte) (0xff & i);
    abyte0[2] = (byte) ((0xff00 & i) >> 8);
    abyte0[1] = (byte) ((0xff0000 & i) >> 16);
    abyte0[0] = (byte) ((0xff000000 & i) >> 24);
    return abyte0;
}


*/

/**
  * 将float转为低字节在前,高字节在后的byte数组
  */
public static byte[] toLH(float f) {
  return toLH(Float.floatToRawIntBits(f));
}

/**
  * 将float转为高字节在前,低字节在后的byte数组
  */
public static byte[] toHH(float f) {
  return toHH(Float.floatToRawIntBits(f));
}

/**
  * 将String转为byte数组
  */
public static byte[] stringToBytes(String s, int length) {
  while (s.getBytes().length < length) {
    s += " ";
  }
  return s.getBytes();
}


/**
  * 将字节数组转换为String
  * @param b byte[]
  * @return String
  */
public static String bytesToString(byte[] b) {
  StringBuffer result = new StringBuffer("");
  int length = b.length;
  for (int i=0; i<length; i++) {
    result.append((char)(b & 0xff));
  }
  return result.toString();
}

/**
  * 将字符串转换为byte数组
  * @param s String
  * @return byte[]
  */
public static byte[] stringToBytes(String s) {
  return s.getBytes();
}

/**
  * 将高字节数组转换为int
  * @param b byte[]
  * @return int
  */
public static int hBytesToInt(byte[] b) {
  int s = 0;
  for (int i = 0; i < 3; i++) {
    if (b >= 0) {
    s = s + b;
    } else {
    s = s + 256 + b;
    }
    s = s * 256;
  }
  if (b[3] >= 0) {
    s = s + b[3];
  } else {
    s = s + 256 + b[3];
  }
  return s;
}

/**
  * 将低字节数组转换为int
  * @param b byte[]
  * @return int
  */
public static int lBytesToInt(byte[] b) {
  int s = 0;
  for (int i = 0; i < 3; i++) {
    if (b[3-i] >= 0) {
    s = s + b[3-i];
    } else {
    s = s + 256 + b[3-i];
    }
    s = s * 256;
  }
  if (b[0] >= 0) {
    s = s + b[0];
  } else {
    s = s + 256 + b[0];
  }
  return s;
}


/**
  * 高字节数组到short的转换
  * @param b byte[]
  * @return short
  */
public static short hBytesToShort(byte[] b) {
  int s = 0;
  if (b[0] >= 0) {
    s = s + b[0];
    } else {
    s = s + 256 + b[0];
    }
    s = s * 256;
  if (b[1] >= 0) {
    s = s + b[1];
  } else {
    s = s + 256 + b[1];
  }
  short result = (short)s;
  return result;
}

/**
  * 低字节数组到short的转换
  * @param b byte[]
  * @return short
  */
public static short lBytesToShort(byte[] b) {
  int s = 0;
  if (b[1] >= 0) {
    s = s + b[1];
    } else {
    s = s + 256 + b[1];
    }
    s = s * 256;
  if (b[0] >= 0) {
    s = s + b[0];
  } else {
    s = s + 256 + b[0];
  }
  short result = (short)s;
  return result;
}

/**
  * 高字节数组转换为float
  * @param b byte[]
  * @return float
  */
public static float hBytesToFloat(byte[] b) {
  int i = 0;
  Float F = new Float(0.0);
  i = ((((b[0]&0xff)<<8 | (b[1]&0xff))<<8) | (b[2]&0xff))<<8 | (b[3]&0xff);
  return F.intBitsToFloat(i);
}

/**
  * 低字节数组转换为float
  * @param b byte[]
  * @return float
  */
public static float lBytesToFloat(byte[] b) {
  int i = 0;
  Float F = new Float(0.0);
  i = ((((b[3]&0xff)<<8 | (b[2]&0xff))<<8) | (b[1]&0xff))<<8 | (b[0]&0xff);
  return F.intBitsToFloat(i);
}

/**
  * 将byte数组中的元素倒序排列
  */
public static byte[] bytesReverseOrder(byte[] b) {
  int length = b.length;
  byte[] result = new byte[length];
  for(int i=0; i<length; i++) {
    result[length-i-1] = b;
  }
  return result;
}

/**
  * 打印byte数组
  */
public static void printBytes(byte[] bb) {
  int length = bb.length;
  for (int i=0; i<length; i++) {
    System.out.print(bb + " ");
  }
  System.out.println("");
}

public static void logBytes(byte[] bb) {
  int length = bb.length;
  String ut = "";
  for (int i=0; i<length; i++) {
    ut = out + bb + " ";
  }

}

/**
  * 将int类型的值转换为字节序颠倒过来对应的int值
  * @param i int
  * @return int
  */
public static int reverseInt(int i) {
  int result = FormatTransfer.hBytesToInt(FormatTransfer.toLH(i));
  return result;
}

/**
  * 将short类型的值转换为字节序颠倒过来对应的short值
  * @param s short
  * @return short
  */
public static short reverseShort(short s) {
  short result = FormatTransfer.hBytesToShort(FormatTransfer.toLH(s));
  return result;
}

/**
  * 将float类型的值转换为字节序颠倒过来对应的float值
  * @param f float
  * @return float
  */
public static float reverseFloat(float f) {
  float result = FormatTransfer.hBytesToFloat(FormatTransfer.toLH(f));
  return result;
}

}

 

你可能感兴趣的:(C++,c,F#,C#,网络协议)