Package com.util; /** * * <ul> * <li> file name: com.born.util.byteutil.java</li> * <li> file Description: Byte conversion tool </li> * <li> Copyright: Copyright (C) 2001-2006</li> * <li> Company: bran</li> * <li> Content Summary:</li> * <li> its
He explained:</li> * <li> completion date:2011-7-18</li> * <li> modification record 0: no </li> * </ul> * * @version 1.0
* @author Xulido */public class Byteutil {/** * convert short to BYTE * * @param b * @param s * need to convert short
* @param index */public static void Putshort (Byte b[], short s, int index) {b[index + 1] = (byte) (S >> 8);
B[index + 0] = (byte) (s >> 0); /** * via byte array to short * * @param b * @param index * @return/public static
Getshort (byte[] B, int index) {return (short) ((B[index + 1] << 8) | B[index + 0] & 0xff)); /** * Convert int to byte array * * @param BB * @param x * @param index */public static voidPutint (byte[] bb, int x, int index) {bb[index + 3] = (byte) (x >> 24);
Bb[index + 2] = (byte) (x >> 16);
Bb[index + 1] = (byte) (x >> 8);
Bb[index + 0] = (byte) (x >> 0); /** * via byte array to int * * * @param BB * @param index * @return/public static int get int (byte[] bb, int index) {return (int) ((Bb[index + 3] & 0xff) << 24 | ((Bb[index + 2] & 0xff) << 16) | ((Bb[index + 1] & 0xff) << 8) |
((Bb[index + 0] & 0xff) << 0)); /** * Convert long to byte array * * @param BB * @param x * @param index */public static void Putlong (byte[] BB, lo
ng x, int index) {bb[index + 7] = (byte) (x >> 56);
Bb[index + 6] = (byte) (x >> 48);
Bb[index + 5] = (byte) (x >> 40);
Bb[index + 4] = (byte) (x >> 32);
Bb[index + 3] = (byte) (x >> 24);
Bb[index + 2] = (byte) (x >> 16);
Bb[index + 1] = (byte) (x >> 8); Bb[index + 0] = (byte) (x >> 0); /** * via byte array to long * * @param BB * @param index * @return/public static long Getlong (byte[] BB, int Index) {return ((((long) Bb[index + 7] & 0xff) << 56) | (((long) Bb[index + 6] & 0xff) << 48) | (((long) Bb[index + 5] & 0xff) << 40) | (((long) Bb[index + 4] & 0xff) << 32) | (((long) Bb[index + 3] & 0xff) << 24) | (((long) Bb[index + 2] & 0xff) << 16) | (((long) Bb[index + 1] & 0xff) << 8) |
(((long) Bb[index + 0] & 0xff) << 0); /** * character to byte conversion * * @param ch * @return/public static void Putchar (byte[] BB, char ch, int index) {int
temp = (int) ch;
Byte[] B = new byte[2]; for (int i = 0; I < 2 i + +) {Bb[index + i] = new Integer (temp & 0xff). Bytevalue ();///save highest bit in lowest bit temp = Te MP >> 8; Move right 8-bit}}/** * byte to character conversion * * @param b * @return/public static char GetCharTe[] B, int index) {int s = 0;
if (B[index + 1] > 0) s + + B[index + 1];
else S + = 256 + b[index + 0];
s *= 256;
if (b[index + 0] > 0) s + + B[index + 1];
else S + = 256 + b[index + 0];
char ch = (char) s;
return ch; /** * Float Convert byte * * @param BB * @param x * @param index */public static void Putfloat (byte[] BB, Floa
t x, int index) {//byte[] B = new Byte[4];
int L = float.floattointbits (x);
for (int i = 0; i < 4; i++) {bb[index + i] = new Integer (l). Bytevalue ();
L = l >> 8; }/** * Get float * * @param BB * @param index * @return/public static float GetFloat (byte[] b via byte array
, int index) {int l;
L = b[index + 0];
L &= 0xFF;
L |= ((long) B[index + 1] << 8);
L &= 0xFFFF;
L |= ((long) B[index + 2] << 16);
L &= 0xFFFFFF;
L |= ((long) B[index + 3] << 24);
Return Float.intbitstofloat (L);
/** * Double Convert byte * * @param BB * @param x * @param index */public static void Putdouble (byte[] BB, double x, int index) {//byte[] B = new Byte[8];
Long L = double.doubletolongbits (x);
for (int i = 0; i < 4; i++) {bb[index + i] = new Long (l). Bytevalue ();
L = l >> 8; }/** * Obtains float * * @param BB * @param index * @return/public static double GetDouble (byte[) by byte array
b, int index) {long L;
L = b[0];
L &= 0xFF;
L |= ((long) b[1] << 8);
L &= 0xFFFF;
L |= ((long) b[2] << 16);
L &= 0xFFFFFF;
L |= ((long) b[3] << 24);
L &= 0XFFFFFFFFL;
L |= ((long) b[4] << 32);
L &= 0XFFFFFFFFFFL;
L |= ((long) b[5] << 40);
L &= 0XFFFFFFFFFFFFL;
L |= ((long) b[6] << 48);
L &= 0XFFFFFFFFFFFFFFL;
L |= ((long) b[7] << 56);
Return double.longbitstodouble (L); }
}
The public class Datatypechangehelper {/** * Converts a single-byte byte to a 32-bit int * * @param b * Byte @return Conve
RT result */public static int Unsignedbytetoint (byte b) {return (int) B & 0xFF; /** * Converts a single-byte byte to a number of 16 binary * * @param b * byte * @return convert result/public static stri
ng Bytetohex (byte b) {int i = b & 0xFF;
return integer.tohexstring (i); /** * Converts a 4byte array to a 32-bit int * * @param buf * bytes buffer * @param byte[] Start the conversion in the position * @return Co
Nvert 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) (Firstbyte<< 24 | Secondbyte << 16 | Thirdbyte << 8 |
fourthbyte)) & 0xFFFFFFFFL; /** * Converts a 16-bit short to a byte array * * @param s * short * @return byte[] length is 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[] Intt
Obytearray (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[] Longtobyt
Earray (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);//secondary low targets[2] = (byt
e) ((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 byte
2int (byte[] res) {/Res = Inversionbyte (res); One byte data left 24 digits to 0x?? 000000, and then move to the right 8 to become 0x00?? 0000 int targets = (Res[0] & 0xff) | ((Res[1] << 8) & 0xff00); // |
Indicates an targets or return; }
}