byte array and short array conversion
Public Short Bytestoshort (byte[] bytes) { return bytebuffer.wrap (bytes). Order (Byteorder.little_endian). Getshort (); } Public byte [] Shorttobytes ( short value) { return bytebuffer.allocate (2) . Order (Byteorder.little_endian). Putshort (value). Array ();
Public short[] Bytearray2shortarray (byte[] data, int items) {short[] RetVal = new Short[items];for (int i = 0; i < Retv Al.length; i++) Retval[i] = (short) ((Data[i * 2] & 0xff) | (Data[i * 2 + 1] & 0xff) << 8); return retVal;}
public class Datatypechangehelper {/** * Converts a single byte of 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 decimal 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 to 32-bit int * * @param buf * bytes buffer * @param byte[] Start Converted position * @return Convert result */public static long Unsigned4bytestoint (byte[] buf, int pos) {in T 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) (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) & 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[] * */PUB Lic static byte[] Longtobytearray (long s) {byte[] 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 & 0xff);//lowest bit targets[1] = (byte) ((res >> 8) & 0xff);//Sub-low TA RGETS[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 a 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?? 0000 int targets = (Res[0] & 0xff) | ((Res[1] << 8) & 0xff00); // | Represents an targets or return; } }
byte array and Int,long,short,byte conversion (reprint)