在socket开发过程中,通常需要将一些具体的值(这些值可能是各种JAVA类型)转化为byte[]类型,为此我总结了如下这个示例,贴出来,以便经常翻看
Java代码
1. public class TestCase {
2.
3.
4. public static byte[] shortToByte(short number) {
5. int temp = number;
6. byte[] b = new byte[2];
7. for (int i = 0; i > 8;// 向右移8位
10. }
11. return b;
12. }
13.
14.
15. public static short byteToShort(byte[] b) {
16. short s = 0;
17. short s0 = (short) (b[0] & 0xff);// 最低位
18. short s1 = (short) (b[1] & 0xff);
19. s1 > 8;// 向右移8位
32. }
33. return b;
34. }
35.
36.
37. public static int byteToInt(byte[] b) {
38. int s = 0;
39. int s0 = b[0] & 0xff;// 最低位
40. int s1 = b[1] & 0xff;
41. int s2 = b[2] & 0xff;
42. int s3 = b[3] & 0xff;
43. s3 > 8;// 向右移8位
58. }
59. return b;
60. }
61.
62.
63. public static long byteToLong(byte[] b) {
64. long s = 0;
65. long s0 = b[0] & 0xff;// 最低位
66. long s1 = b[1] & 0xff;
67. long s2 = b[2] & 0xff;
68. long s3 = b[3] & 0xff;
69. long s4 = b[4] & 0xff;// 最低位
70. long s5 = b[5] & 0xff;
71. long s6 = b[6] & 0xff;
72. long s7 = b[7] & 0xff;
73.
74. // s0不变
75. s1 > 8;
93. }
94. return b;
95. }
96.
97.
98. public static double getDouble(byte[] b) {
99. long m;
100. m = b[0];
101. m &= 0xff;
102. m |= ((long) b[1] > 8);
138. b[1] = (byte) (c & 0xFF);
139. return b;
140. }
141.
142.
143. public static char byteToChar(byte[] b){
144. char c = (char) (((b[0] & 0xFF)
Java代码
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net