Tag:byte array data conversion
/** * Converts a single byte byte to a 32-bit int * * @param b * byte * @return convert result */public static int unsignedbytetoint (Byte b) {return (int) b & 0xFF;} /** * Converts a single byte byte to 16 binary number * * @param b * byte * @return convert result */public static String Bytetohex (byte b) {int i = B & 0xff;return integer.tohexstring (i);} /** * Converts an array of 4byte into a 32-bit int * * @param buf * bytes buffer * @param byte[] Where to start converting * @return convert result */p Ublic Static Long Unsigned4bytestoint (byte[] buf, int pos) {int firstbyte = 0;int Secondbyte = 0;int Thirdbyte = 0;int Fou Rthbyte = 0;int index = Pos;firstbyte = (0X000000FF & ((int) buf[index])); Secondbyte = (0X000000FF & ((int) buf[in Dex + 1]); thirdbyte = (0X000000FF & ((int) Buf[index + 2]); Fourthbyte = (0X000000FF & ((int) Buf[index + 3])); in Dex = index + 4;return ((long) (Firstbyte << | secondbyte << thirdbyte << 8 | fourthbyte)) & 0xFFFFFFFFL;} /** * Converts a 16-bit short to a byte array * * @param s * Short * @return byte[] Length 2 * */public static byte[] Shorttobytearray (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) &A mp 0xFF);} return targets;} /** * Converts a 32-bit integer to a byte array of length 4 * * @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) {by Te[] targets = new Byte[2];for (int i = 0; i < 8; i++) {int 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 & 0x FF);//Lowest bitTARGETS[1] = (byte) ((res >> 8) & 0xff),//Sub-low targets[2] = (byte) ((res >> +) & 0xff);//Secondary High targets[3 ] = (byte) (res >>> 24);//highest bit, unsigned right shift. return targets;} /** * Converts a byte array of length 2 to 16-bit int * * @param res * byte[] * @return int * */public static int byte2int (byte[] res) { res = Inversionbyte (res);//A byte data shifted left 24 bits into 0x?? 000000, then move right 8 bits into 0x00?? 0000int targets = (Res[0] & 0xff) | ((Res[1] << 8) & 0xff00); // | Represents the position or return targets;} /** * byte array and short array conversion * * @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;} /** * byte array and short array conversion * * @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) {RET Val[i] = (byte) (DATA[I/2]);} else {Retval[i] = (byte) (DATA[I/2] >> 8);}} return retVal;} /** * object to Array * * @param obj * @return */public static byte[] Tobytearray (object obj) {byte[] bytes = NULL; Bytearrayoutputstream BOS = new Bytearrayoutputstream (); try {objectoutputstream oos = new ObjectOutputStream (BOS); O Os.writeobject (obj); oos.flush (); bytes = Bos.tobytearray (); Oos.close (); Bos.close ();} catch (IOException ex) {ex.printstacktrace ();} return bytes;} /** * Array to Object * * @param bytes * @return */public Static object Toobject (byte[] bytes) {Object obj = null;try {BYTEARRAYINP Utstream 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;}
byte array conversion in Android