天天看点

android中的byte数组转换(转)

1 /**       2      * 将一个单字节的byte转换成32位的int       3      *        4      * @param b       5      *            byte       6      * @return convert result       7      */       8     public static int unsignedByteToInt(byte b) {       9         return (int) b & 0xFF;      10     }      11        12     /**      13      * 将一个单字节的Byte转换成十六进制的数      14      *       15      * @param b      16      *            byte      17      * @return convert result      18      */      19     public static String byteToHex(byte b) {      20         int i = b & 0xFF;      21         return Integer.toHexString(i);      22     }      23        24     /**      25      * 将一个4byte的数组转换成32位的int      26      *       27      * @param buf      28      *            bytes buffer      29      * @param byte[]中开始转换的位置      30      * @return convert result      31      */      32     public static long unsigned4BytesToInt(byte[] buf, int pos) {      33         int firstByte = 0;      34         int secondByte = 0;      35         int thirdByte = 0;      36         int fourthByte = 0;      37         int index = pos;      38         firstByte = (0x000000FF & ((int) buf[index]));      39         secondByte = (0x000000FF & ((int) buf[index + 1]));      40         thirdByte = (0x000000FF & ((int) buf[index + 2]));      41         fourthByte = (0x000000FF & ((int) buf[index + 3]));      42         index = index + 4;      43         return ((long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL;      44     }      45        46     /**      47      * 将16位的short转换成byte数组      48      *       49      * @param s      50      *            short      51      * @return byte[] 长度为2      52      * */      53     public static byte[] shortToByteArray(short s) {      54         byte[] targets = new byte[2];      55         for (int i = 0; i < 2; i++) {      56             int offset = (targets.length - 1 - i) * 8;      57             targets[i] = (byte) ((s >>> offset) & 0xff);      58         }      59         return targets;      60     }      61        62     /**      63      * 将32位整数转换成长度为4的byte数组      64      *       65      * @param s      66      *            int      67      * @return byte[]      68      * */      69     public static byte[] intToByteArray(int s) {      70         byte[] targets = new byte[2];      71         for (int i = 0; i < 4; i++) {      72             int offset = (targets.length - 1 - i) * 8;      73             targets[i] = (byte) ((s >>> offset) & 0xff);      74         }      75         return targets;      76     }      77        78     /**      79      * long to byte[]      80      *       81      * @param s      82      *            long      83      * @return byte[]      84      * */      85     public static byte[] longToByteArray(long s) {      86         byte[] targets = new byte[2];      87         for (int i = 0; i < 8; i++) {      88             int offset = (targets.length - 1 - i) * 8;      89             targets[i] = (byte) ((s >>> offset) & 0xff);      90         }      91         return targets;      92     }      93        94     /** 32位int转byte[] */      95     public static byte[] int2byte(int res) {      96         byte[] targets = new byte[4];      97         targets[0] = (byte) (res & 0xff);// 最低位      98         targets[1] = (byte) ((res >> 8) & 0xff);// 次低位      99         targets[2] = (byte) ((res >> 16) & 0xff);// 次高位     100         targets[3] = (byte) (res >>> 24);// 最高位,无符号右移。     101         return targets;     102     }     103       104     /**     105      * 将长度为2的byte数组转换为16位int     106      *      107      * @param res     108      *            byte[]     109      * @return int     110      * */     111     public static int byte2int(byte[] res) {     112         // res = InversionByte(res);     113         // 一个byte数据左移24位变成0x??000000,再右移8位变成0x00??0000     114         int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00); // | 表示安位或     115         return targets;     116     }     117       118     /**     119      * byte数组与short数组转换     120      *      121      * @param data     122      * @param items     123      * @return     124      */     125     public static short[] byteArray2ShortArray(byte[] data) {     126         if (data == null) {     127             // Log.e(TAG, "byteArray2ShortArray, data = null");     128             return null;     129         }     130       131         short[] retVal = new short[data.length / 2];     132         for (int i = 0; i < retVal.length; i++) {     133             retVal[i] = (short) ((data[i * 2] & 0xff) | (data[i * 2 + 1] & 0xff) << 8);     134         }     135       136         return retVal;     137     }     138       139     /**     140      * byte数组与short数组转换     141      *      142      * @param data     143      * @param items     144      * @return     145      */     146     public static byte[] shortArray2ByteArray(short[] data) {     147         byte[] retVal = new byte[data.length * 2];     148         for (int i = 0; i < retVal.length; i++) {     149             int mod = i % 2;     150             if (mod == 0) {     151                 retVal[i] = (byte) (data[i / 2]);     152             } else {     153                 retVal[i] = (byte) (data[i / 2] >> 8);     154             }     155         }     156       157         return retVal;     158     }     159       160     /**     161      * 对象转数组     162      *      163      * @param obj     164      * @return     165      */     166     public static byte[] toByteArray(Object obj) {     167         byte[] bytes = null;     168         ByteArrayOutputStream bos = new ByteArrayOutputStream();     169         try {     170             ObjectOutputStream oos = new ObjectOutputStream(bos);     171             oos.writeObject(obj);     172             oos.flush();     173             bytes = bos.toByteArray();     174             oos.close();     175             bos.close();     176         } catch (IOException ex) {     177             ex.printStackTrace();     178         }     179         return bytes;     180     }     181       182     /**     183      * 数组转对象     184      *      185      * @param bytes     186      * @return     187      */     188     public static Object toObject(byte[] bytes) {     189         Object obj = null;     190         try {     191             ByteArrayInputStream bis = new ByteArrayInputStream(bytes);     192             ObjectInputStream ois = new ObjectInputStream(bis);     193             obj = ois.readObject();     194             ois.close();     195             bis.close();     196         } catch (IOException ex) {     197             ex.printStackTrace();     198         } catch (ClassNotFoundException ex) {     199             ex.printStackTrace();     200         }     201         return obj;     202     }