java字节序

 以下对BIG-ENDIAN、LITTLE-ENDIAN的解释正好弄反了!!!!
  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,所以经常出现数据传输错误。
  5.文章开头所提出的问题,就是因为程序运行在X86架构的PC SERVER上,发送数据的一端用C实现的,接收一端是用JAVA实现的,而发送端在发送数据前未进行从主机字节序到网络字节序的转换,这样接收端接收到的是LITTLE-ENDIAN的数据,数据解释自然出错。
  具体数据如下,实际发送的数据为23578
  发送端发送数据: 1A 5C
  接收端接收到数据后,按BIG-ENDIAN进行解释具体数据是多少?你们自己去计算并比较吧!
  ===============================================================================================
  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方式存储数据是符合我们人类的思维习惯的。而little 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中提供了四个转换字节序的宏。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值