0
点赞
收藏
分享

微信扫一扫

byte[]与各种数据类型互相转换示例

罗蓁蓁 2023-04-02 阅读 44


在socket开发过程中,通常需要将一些具体的值(这些值可能是各种JAVA类型)转化为byte[]类型,为此我总结了如下这个示例,贴出来,以便经常翻看




Java代码

byte[]与各种数据类型互相转换示例_System


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 < b.length; i++) {   
8.            b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位   
9.            temp = temp >> 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;   
20.        s = (short) (s0 | s1);   
21.        return s;   
22.    }   
23.       
24.       
25.      
26.    public static byte[] intToByte(int number) {   
27.        int temp = number;   
28.        byte[] b = new byte[4];   
29.        for (int i = 0; i < b.length; i++) {   
30.            b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位   
31.            temp = temp >> 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 <<= 24;   
44.        s2 <<= 16;   
45.        s1 <<= 8;   
46.        s = s0 | s1 | s2 | s3;   
47.        return s;   
48.    }   
49.       
50.       
51.      
52.    public static byte[] longToByte(long number) {   
53.        long temp = number;   
54.        byte[] b = new byte[8];   
55.        for (int i = 0; i < b.length; i++) {   
56.            b[i] = new Long(temp & 0xff).byteValue();// 将最低位保存在最低位 temp = temp   
57.                                                        // >> 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;   
76.        s2 <<= 16;   
77.        s3 <<= 24;   
78.        s4 <<= 8 * 4;   
79.        s5 <<= 8 * 5;   
80.        s6 <<= 8 * 6;   
81.        s7 <<= 8 * 7;   
82.        s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;   
83.        return s;   
84.    }   
85.       
86.      
87.    public static byte[] doubleToByte(double num) {     
88.        byte[] b = new byte[8];     
89.        long l = Double.doubleToLongBits(num);     
90.        for (int i = 0; i < 8; i++) {     
91.            b[i] = new Long(l).byteValue();     
92.            l = l >> 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);     
103.        m &= 0xffff;     
104.        m |= ((long) b[2] << 16);     
105.        m &= 0xffffff;     
106.        m |= ((long) b[3] << 24);     
107.        m &= 0xffffffffl;     
108.        m |= ((long) b[4] << 32);     
109.        m &= 0xffffffffffl;     
110.        m |= ((long) b[5] << 40);     
111.        m &= 0xffffffffffffl;     
112.        m |= ((long) b[6] << 48);     
113.        m &= 0xffffffffffffffl;     
114.        m |= ((long) b[7] << 56);     
115.        return Double.longBitsToDouble(m);     
116.    }   
117.       
118.       
119.      
120.    public static void floatToByte(float x) {   
121.        //先用 Float.floatToIntBits(f)转换成int   
122.    }   
123.       
124.      
125.    public static float getFloat(byte[] b) {     
126.        // 4 bytes     
127.        int accum = 0;     
128.        for ( int shiftBy = 0; shiftBy < 4; shiftBy++ ) {     
129.                accum |= (b[shiftBy] & 0xff) << shiftBy * 8;     
130.        }     
131.        return Float.intBitsToFloat(accum);     
132.    }     
133.  
134.       
135.     public static byte[] charToByte(char c){   
136.        byte[] b = new byte[2];   
137.        b[0] = (byte) ((c & 0xFF00) >> 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) << 8) | (b[1] & 0xFF));   
145.        return c;   
146.     }   
147.       
148.      
149.    public static byte[] stringToByte(String str) throws UnsupportedEncodingException{   
150.        return str.getBytes("GBK");   
151.    }   
152.       
153.      
154.    public static String bytesToString(byte[] str) {   
155.        String keyword = null;   
156.        try {   
157.            keyword = new String(str,"GBK");   
158.        } catch (UnsupportedEncodingException e) {   
159.            e.printStackTrace();   
160.        }   
161.        return keyword;   
162.    }   
163.       
164.       
165.      
166.    @Test  
167.    public void testObject2ByteArray() throws IOException,   
168.            ClassNotFoundException {   
169.        // Object obj = "";   
170.        Integer[] obj = { 1, 3, 4 };   
171.  
172.        // // object to bytearray   
173.        ByteArrayOutputStream bo = new ByteArrayOutputStream();   
174.        ObjectOutputStream oo = new ObjectOutputStream(bo);   
175.        oo.writeObject(obj);   
176.        byte[] bytes = bo.toByteArray();   
177.        bo.close();   
178.        oo.close();   
179.        System.out.println(Arrays.toString(bytes));   
180.  
181.        Integer[] intArr = (Integer[]) testByteArray2Object(bytes);   
182.        System.out.println(Arrays.asList(intArr));   
183.  
184.  
185.        byte[] b2 = intToByte(123);   
186.        System.out.println(Arrays.toString(b2));   
187.  
188.        int a = byteToInt(b2);   
189.        System.out.println(a);   
190.  
191.    }   
192.  
193.      
194.    private Object testByteArray2Object(byte[] bytes) throws IOException,   
195.            ClassNotFoundException {   
196.        // byte[] bytes = null;   
197.        Object obj;   
198.        // bytearray to object   
199.        ByteArrayInputStream bi = new ByteArrayInputStream(bytes);   
200.        ObjectInputStream oi = new ObjectInputStream(bi);   
201.        obj = oi.readObject();   
202.        bi.close();   
203.        oi.close();   
204.        System.out.println(obj);   
205.        return obj;   
206.    }   
207.  
208. }

Java代码

byte[]与各种数据类型互相转换示例_System

举报

相关推荐

0 条评论