Java class Library support is not as good as C #, such as the time class, such as data type conversion class and so on, I did not find it?
The following is the byte conversion class, byte[] with short, int, long, float, double, string conversion, network byte order htons, HTONL, etc. byte[] to hexadecimal string, binary string implementation.
Hope to be useful to friends, if there is a better way, please remind me.
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); /** * takes a byte array to short * * @param b * @param index * starts with * @return */public static short Getshort (byte[] B, int. index) {return (short) (((B[index + 1] << 8) | b[ind Ex + 0] & 0xff)); }/** * Convert int to byte array * * @param BB * @param x * @param index */public static V OID putint (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); }/** * takes an int * * through a byte array @paRAM BB * @param index * start * @return */public static int getInt (byte[] BB, int ind Ex) {return (int) (((Bb[index + 3] & 0xff) << 24) | ((Bb[index + 2] & 0xff) << 16) | ((Bb[index + 1] & 0xff) << 8) | ((Bb[index + 0] & 0xff) << 0))); }/** * Converts a long type to a byte array * * @param BB * @param x * @param index */public static void Putlong (byte[] BB, long 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); }/** * through 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, cha R 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 ();//Keep the highest bit in the lowest bit temp = temp >> 8; Move Right 8 bits}}/** * byte-to-character conversion * * @param b * @return */public static char GetChar (byte[] B , int index) {int s = 0; if (B[index + 1] > 0) s + = B[index + 1]; else S + = + b[index + 0]; s *= 256; if (b[index + 0] > 0) s + = B[index + 1]; else S + = + b[index + 0]; char ch = (char) s; return ch; }/** * Float convert byte * * @param BB * @param x * @param index */public static VO ID putfloat (byte[] BB, float 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 via byte array static float GetFloat (byte[] b, 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 V OID putdouble (byte[] BB, double x, int index) {//byte[] B = new Byte[8]; Long L = double.doubletolongbits (x); for (int i = 0; i < 8; i++) {bb[index + i] = new Long (l). Bytevalue (); L = l >> 8; }}/** * Get double * * @param BB * @param index * @return */publi by byte array C Static Double getdouble (byte[] 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); public static void Puthexstring (byte[] BB, String S, int index) {for (int i = 0; i < s.length (); i+=2) { byte c= (Byte) (Chartobyte (S.charat (i)) <<4 | Chartobyte (S.charat (i+1))); bb[index+ (i>>1)] = C; }} private static Byte Chartobyte (char c) {return (byte) "0123456789ABCDEF". IndexOf (c); } public static String gethexstring (byte[] B, int index, int count) {StringBuilder StringBuilder = new S Tringbuilder (""); If(b = = NULL | | Index < 0 | | B.length < index + count) {return null; } for (int i = index; i < count + index; i++) {int v = b[i] & 0xFF; String HV = integer.tohexstring (v); if (Hv.length () < 2) {stringbuilder.append (0); } stringbuilder.append (HV); } return stringbuilder.tostring (); } public static String getbinarystring (int d,int length) {StringBuilder StringBuilder = new StringBuilder ( ""); String HV = integer.tobinarystring (d); for (int j = 0; J < Length-hv.length (); j + +) {stringbuilder.append (0); } stringbuilder.append (HV); return stringbuilder.tostring (); } public static String getbinaryreversestring (int d,int length) {StringBuilder StringBuilder = new Stringbui Lder (""); String HV = integer.tobinarystring (d); For (int j = 0; J < Length-hv.length (); j + +) {stringbuilder.append (0); } stringbuilder.append (HV); Stringbuilder.reverse (); return stringbuilder.tostring (); }//network byte reverse public static byte[] Reversendian (Byte b[],int count, Boolean big) {byte by; byte data[] = new Byte[count]; for (int i=0;i<count;i++) {Data[i] = B[i]; } if (Big==false) {for (int. i=0;i<count;i++) {by = B[i]; Data[count-i-1] = by; }} return data; } public static short htons (short s) {short rslt = 0; byte [] bs1 = new byte[2]; Byteutil.putshort (BS1, S, 0); byte[] BS2 = Reversendian (BS1, 2, false); Rslt = Byteutil.getshort (BS2, 0); return rslt; } public static int htonl (int d) {int rslt = 0; byte [] bs1 = new Byte[4]; ByteutiL.putint (BS1, D, 0); byte[] BS2 = Reversendian (BS1, 4, false); Rslt = Byteutil.getint (BS2, 0); return rslt; }}
Java byte transformation class implementation