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


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

    Java代码  收藏代码
    1. public class TestCase {  
    2.       
    3.     /** 
    4.      * short到字节数组的转换. 
    5.      */  
    6.     public static byte[] shortToByte(short number) {  
    7.         int temp = number;  
    8.         byte[] b = new byte[2];  
    9.         for (int i = 0; i < b.length; i++) {  
    10.             b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位  
    11.             temp = temp >> 8;// 向右移8位  
    12.         }  
    13.         return b;  
    14.     }  
    15.   
    16.     /** 
    17.      * 字节数组到short的转换. 
    18.      */  
    19.     public static short byteToShort(byte[] b) {  
    20.         short s = 0;  
    21.         short s0 = (short) (b[0] & 0xff);// 最低位  
    22.         short s1 = (short) (b[1] & 0xff);  
    23.         s1 <<= 8;  
    24.         s = (short) (s0 | s1);  
    25.         return s;  
    26.     }  
    27.       
    28.       
    29.     /** 
    30.      * int到字节数组的转换. 
    31.      */  
    32.     public static byte[] intToByte(int number) {  
    33.         int temp = number;  
    34.         byte[] b = new byte[4];  
    35.         for (int i = 0; i < b.length; i++) {  
    36.             b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位  
    37.             temp = temp >> 8;// 向右移8位  
    38.         }  
    39.         return b;  
    40.     }  
    41.   
    42.     /** 
    43.      * 字节数组到int的转换. 
    44.      */  
    45.     public static int byteToInt(byte[] b) {  
    46.         int s = 0;  
    47.         int s0 = b[0] & 0xff;// 最低位  
    48.         int s1 = b[1] & 0xff;  
    49.         int s2 = b[2] & 0xff;  
    50.         int s3 = b[3] & 0xff;  
    51.         s3 <<= 24;  
    52.         s2 <<= 16;  
    53.         s1 <<= 8;  
    54.         s = s0 | s1 | s2 | s3;  
    55.         return s;  
    56.     }  
    57.       
    58.       
    59.     /** 
    60.      * long类型转成byte数组 
    61.      */  
    62.     public static byte[] longToByte(long number) {  
    63.         long temp = number;  
    64.         byte[] b = new byte[8];  
    65.         for (int i = 0; i < b.length; i++) {  
    66.             b[i] = new Long(temp & 0xff).byteValue();// 将最低位保存在最低位 temp = temp  
    67.                                                         // >> 8;// 向右移8位  
    68.         }  
    69.         return b;  
    70.     }  
    71.   
    72.     /** 
    73.      * 字节数组到long的转换. 
    74.      */  
    75.     public static long byteToLong(byte[] b) {  
    76.         long s = 0;  
    77.         long s0 = b[0] & 0xff;// 最低位  
    78.         long s1 = b[1] & 0xff;  
    79.         long s2 = b[2] & 0xff;  
    80.         long s3 = b[3] & 0xff;  
    81.         long s4 = b[4] & 0xff;// 最低位  
    82.         long s5 = b[5] & 0xff;  
    83.         long s6 = b[6] & 0xff;  
    84.         long s7 = b[7] & 0xff;  
    85.   
    86.         // s0不变  
    87.         s1 <<= 8;  
    88.         s2 <<= 16;  
    89.         s3 <<= 24;  
    90.         s4 <<= 8 * 4;  
    91.         s5 <<= 8 * 5;  
    92.         s6 <<= 8 * 6;  
    93.         s7 <<= 8 * 7;  
    94.         s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;  
    95.         return s;  
    96.     }  
    97.       
    98.     /** 
    99.      * double到字节数组的转换. 
    100.      */  
    101.     public static byte[] doubleToByte(double num) {    
    102.         byte[] b = new byte[8];    
    103.         long l = Double.doubleToLongBits(num);    
    104.         for (int i = 0; i < 8; i++) {    
    105.             b[i] = new Long(l).byteValue();    
    106.             l = l >> 8;    
    107.         }  
    108.         return b;  
    109.     }  
    110.       
    111.     /** 
    112.      * 字节数组到double的转换. 
    113.      */  
    114.     public static double getDouble(byte[] b) {    
    115.         long m;    
    116.         m = b[0];    
    117.         m &= 0xff;    
    118.         m |= ((long) b[1] << 8);    
    119.         m &= 0xffff;    
    120.         m |= ((long) b[2] << 16);    
    121.         m &= 0xffffff;    
    122.         m |= ((long) b[3] << 24);    
    123.         m &= 0xffffffffl;    
    124.         m |= ((long) b[4] << 32);    
    125.         m &= 0xffffffffffl;    
    126.         m |= ((long) b[5] << 40);    
    127.         m &= 0xffffffffffffl;    
    128.         m |= ((long) b[6] << 48);    
    129.         m &= 0xffffffffffffffl;    
    130.         m |= ((long) b[7] << 56);    
    131.         return Double.longBitsToDouble(m);    
    132.     }  
    133.       
    134.       
    135.     /** 
    136.      * float到字节数组的转换. 
    137.      */  
    138.     public static void floatToByte(float x) {  
    139.         //先用 Float.floatToIntBits(f)转换成int  
    140.     }  
    141.       
    142.     /** 
    143.      * 字节数组到float的转换. 
    144.      */  
    145.     public static float getFloat(byte[] b) {    
    146.         // 4 bytes    
    147.         int accum = 0;    
    148.         for ( int shiftBy = 0; shiftBy < 4; shiftBy++ ) {    
    149.                 accum |= (b[shiftBy] & 0xff) << shiftBy * 8;    
    150.         }    
    151.         return Float.intBitsToFloat(accum);    
    152.     }    
    153.   
    154.      /** 
    155.       * char到字节数组的转换. 
    156.       */  
    157.      public static byte[] charToByte(char c){  
    158.         byte[] b = new byte[2];  
    159.         b[0] = (byte) ((c & 0xFF00) >> 8);  
    160.         b[1] = (byte) (c & 0xFF);  
    161.         return b;  
    162.      }  
    163.        
    164.      /** 
    165.       * 字节数组到char的转换. 
    166.       */  
    167.      public static char byteToChar(byte[] b){  
    168.         char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));  
    169.         return c;  
    170.      }  
    171.       
    172.     /** 
    173.      * string到字节数组的转换. 
    174.      */  
    175.     public static byte[] stringToByte(String str) throws UnsupportedEncodingException{  
    176.         return str.getBytes("GBK");  
    177.     }  
    178.       
    179.     /** 
    180.      * 字节数组到String的转换. 
    181.      */  
    182.     public static String bytesToString(byte[] str) {  
    183.         String keyword = null;  
    184.         try {  
    185.             keyword = new String(str,"GBK");  
    186.         } catch (UnsupportedEncodingException e) {  
    187.             e.printStackTrace();  
    188.         }  
    189.         return keyword;  
    190.     }  
    191.       
    192.       
    193.     /** 
    194.      * object到字节数组的转换 
    195.      */  
    196.     @Test  
    197.     public void testObject2ByteArray() throws IOException,  
    198.             ClassNotFoundException {  
    199.         // Object obj = "";  
    200.         Integer[] obj = { 1, 3, 4 };  
    201.   
    202.         // // object to bytearray  
    203.         ByteArrayOutputStream bo = new ByteArrayOutputStream();  
    204.         ObjectOutputStream oo = new ObjectOutputStream(bo);  
    205.         oo.writeObject(obj);  
    206.         byte[] bytes = bo.toByteArray();  
    207.         bo.close();  
    208.         oo.close();  
    209.         System.out.println(Arrays.toString(bytes));  
    210.   
    211.         Integer[] intArr = (Integer[]) testByteArray2Object(bytes);  
    212.         System.out.println(Arrays.asList(intArr));  
    213.   
    214.   
    215.         byte[] b2 = intToByte(123);  
    216.         System.out.println(Arrays.toString(b2));  
    217.   
    218.         int a = byteToInt(b2);  
    219.         System.out.println(a);  
    220.   
    221.     }  
    222.   
    223.     /** 
    224.      * 字节数组到object的转换. 
    225.      */  
    226.     private Object testByteArray2Object(byte[] bytes) throws IOException,  
    227.             ClassNotFoundException {  
    228.         // byte[] bytes = null;  
    229.         Object obj;  
    230.         // bytearray to object  
    231.         ByteArrayInputStream bi = new ByteArrayInputStream(bytes);  
    232.         ObjectInputStream oi = new ObjectInputStream(bi);  
    233.         obj = oi.readObject();  
    234.         bi.close();  
    235.         oi.close();  
    236.         System.out.println(obj);  
    237.         return obj;  
    238.     }  
    239.   
    240. }  
  • 相关阅读:
    利用反射技术修改类中的字段(成员变量的反射)
    Java长存!12个Java长久占居主要地位的原因
    撰写架构设计文档的心得体会
    做个正能量的程序员
    程序员如何提高自己的编程水平
    mysql查询优化
    MySQL修改最大连接数,没有my.ini文件,只有my-default,这怎么改呀?
    PDO 拿出來的 Float 數據跟数据库中的数据不匹配
    大量多级分类数据的获取、缓存、搜索查询 怎么设计最快 ?
    windows下MySQL5.6以上版本,如何通过修改配置文件来修改数据库的最大连接数啊?
  • 原文地址:https://www.cnblogs.com/wzhanke/p/4562056.html
Copyright © 2020-2023  润新知