Byte array conversion in android
/*** Convert a single byte to a 32-bit int ** @ param B * byte * @ return convert result */public static int unsignedByteToInt (byte B) {return (int) B & 0xFF ;} /*** convert a single Byte to a hexadecimal number ** @ param B * byte * @ return convert result */public static String byteToHex (byte B) {int I = B & 0xFF; return Integer. toHexString (I );} /*** convert a 4 byte array to a 32-bit int ** @ param buf * bytes buffer * @ param byte [] to start the conversion position * @ return conv Ert result */public static long unsigned4BytesToInt (byte [] buf, int pos) {int firstByte = 0; int secondByte = 0; int thirdByte = 0; int fourthByte = 0; int index = pos; firstByte = (0x000000FF & (int) buf [index]); secondByte = (0x000000FF & (int) buf [index + 1]); thirdByte = (0x000000FF & (int) buf [index + 2]); fourthByte = (0x000000FF & (int) buf [index + 3]); index = index + 4; return (long) (firs TByte <24 | secondByte <16 | thirdByte <8 | fourthByte) & 0 xFFFFFFFFL ;} /*** convert a 16-bit short to a byte array. ** @ param s * short * @ return byte [] the length is 2 **/public static byte [] bytes tobytearray (short s) {byte [] targets = new byte [2]; for (int I = 0; I <2; I ++) {int offset = (targets. length-1-I) * 8; targets [I] = (byte) (s >>> offset) & 0xff);} return targets ;} /*** convert a 32-bit integer to a 4-byte array. ** @ param s * Int * @ return byte [] **/public static byte [] intToByteArray (int s) {byte [] targets = new byte [2]; for (int I = 0; I <4; I ++) {int offset = (targets. length-1-I) * 8; targets [I] = (byte) (s >>> offset) & 0xff);} return targets ;} /*** long to byte [] ** @ param s * long * @ return byte [] **/public static byte [] longToByteArray (long s) {byte [] targets = new byte [2]; for (int I = 0; I <8; I ++) {in T offset = (targets. length-1-I) * 8; targets [I] = (byte) (s >>> offset) & 0xff);} return targets ;} /** 32-bit int to byte [] */public static byte [] int2byte (int res) {byte [] targets = new byte [4]; targets [0] = (byte) (res & 0xff); // second bit targets [1] = (byte) (res> 8) & 0xff ); // low position targets [2] = (byte) (res> 16) & 0xff); // high position targets [3] = (byte) (res> 24); // The highest bit, unsigned shifted to the right. Return targets ;} /*** convert the byte array with a length of 2 to a 16-bit int ** @ param res * byte [] * @ return int **/public static int byte2int (byte [] res) {// res = InversionByte (res); // shifts the value of a byte to a value of 0x ?? 000000, then shift the eight digits to 0x00 ?? Optional int targets = (res [0] & 0xff) | (res [1] <8) & 0xff00); // | indicates an AMAP or return targets ;} /*** convert byte array and short array ** @ param data * @ param items * @ return */public static short [] byteArray2ShortArray (byte [] data) {if (data = null) {// Log. e (TAG, "byteArray2ShortArray, data = null"); return null;} short [] retVal = new short [data. length/2]; for (int I = 0; I <retVal. length; I ++) {retVal [I] = (short) (data [I * 2] & 0xff) | (data [I * 2 + 1] & 0xff) <8) ;}return retVal ;} /*** convert byte array and short array ** @ param data * @ param items * @ return */public static byte [] shortArray2ByteArray (short [] data) {byte [] retVal = new byte [data. length * 2]; for (int I = 0; I <retVal. length; I ++) {int mod = I % 2; if (mod = 0) {retVal [I] = (byte) (data [I/2]);} else {retVal [I] = (byte) (data [I/2]> 8) ;}} return retVal ;} /*** convert an Object to an array ** @ param obj * @ return */public static byte [] toByteArray (Object obj) {byte [] bytes = null; byteArrayOutputStream bos = new ByteArrayOutputStream (); try {ObjectOutputStream oos = new ObjectOutputStream (bos); oos. writeObject (obj); oos. flush (); bytes = bos. toByteArray (); oos. close (); bos. close ();} catch (IOException ex) {ex. printStackTrace ();} return bytes;}/*** convert an array to an Object ** @ param bytes * @ return */public static Object toObject (byte [] bytes) {Object obj = null; try {ByteArrayInputStream bis = new ByteArrayInputStream (bytes); ObjectInputStream ois = new ObjectInputStream (bis); obj = ois. readObject (); ois. close (); bis. close ();} catch (IOException ex) {ex. printStackTrace ();} catch (ClassNotFoundException ex) {ex. printStackTrace ();} return obj ;}