0
点赞
收藏
分享

微信扫一扫

ByteTool.java



public class ByteTool {

/**

* byte to int

*

* @param b

* 待转换的字节数组

* @param offset

* 偏移量,字节数组中开始转换的位置

* @return

*/

public static int byte2int(byte b[], int offset) {

return b[offset + 3] & 0xff | (b[offset + 2] & 0xff) << 8

| (b[offset + 1] & 0xff) << 16 | (b[offset] & 0xff) << 24;

}

public static short byte2short(byte b[], int offset) {

return (short)(b[offset + 1] & 0xff | (b[offset] & 0xff) << 8);

}


/**

* int to byte

*

* @param n待转换的整形变量

* @param buf

* 转换后生成的字节数组

* @param offset

* 偏移量,字节数组中开始存放的位置

*/

public static void int2byte(int n, byte buf[], int offset) {

buf[offset] = (byte) (n >> 24);

buf[offset + 1] = (byte) (n >> 16);

buf[offset + 2] = (byte) (n >> 8);

buf[offset + 3] = (byte) n;

}


/**

* @returntype void

* @param n

* 待转换的short变量

* @param buf

* 转换后存放的byte数组

* @param offset偏移量,字节数组中开始存放的位置

*/

public static void short2byte(int n, byte buf[], int offset) {

buf[offset] = (byte) (n >> 8);

buf[offset + 1] = (byte) n;

}


/**

*

* @param buf

* @return

*/

public static String byte2Hex(byte[] buf) {

StringBuffer sb = new StringBuffer();

//sb.append("{");

for (byte b : buf) {

if (b == 0) {

sb.append("00");

} else if (b == -1) {

sb.append("FF");

} else {

String str = Integer.toHexString(b).toUpperCase();

// sb.append(a);

if (str.length() == 8) {

str = str.substring(6, 8);

} else if (str.length() < 2) {

str = "0" + str;

}

sb.append(str);


}

//sb.append(" ");

}

//sb.append("}");

return sb.toString();

}


public static int unsignedByteToInt(byte b) {

return (int) b & 0xFF;

}


/**

* convert signed one byte into a hexadecimal digit

*

* @param b

* byte

* @return convert result

*/

public static String byteToHex(byte b) {

int i = b & 0xFF;

return Integer.toHexString(i);

}


/**

* convert signed 4 bytes into a 32-bit integer

*

* @param buf

* bytes buffer

* @param pos

* beginning <code>byte</code>> for converting

* @return convert 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;

}


public static long bytes2long(byte[] b) {


int mask = 0xff;

long temp = 0;

long res = 0;

for (int i = 0; i < 8; i++) {

res <<= 8;

temp = b[i] & mask;

res |= temp;

}

return res;

}


public static byte[] long2bytes(long num) {

byte[] b = new byte[8];

for (int i = 0; i < 8; i++) {

b[i] = (byte) (num >>> (56 - i * 8));

}

return b;

}


public static long getLong(byte[] bb, int index) {

return ((((long) bb[index + 0] & 0xff) << 56)

| (((long) bb[index + 1] & 0xff) << 48)

| (((long) bb[index + 2] & 0xff) << 40)

| (((long) bb[index + 3] & 0xff) << 32)

| (((long) bb[index + 4] & 0xff) << 24)

| (((long) bb[index + 5] & 0xff) << 16)

| (((long) bb[index + 6] & 0xff) << 8) | (((long) bb[index + 7] & 0xff) << 0));

}


public static void putLong(byte[] bb, long x, int index) {

bb[index + 0] = (byte) (x >> 56);

bb[index + 1] = (byte) (x >> 48);

bb[index + 2] = (byte) (x >> 40);

bb[index + 3] = (byte) (x >> 32);

bb[index + 4] = (byte) (x >> 24);

bb[index + 5] = (byte) (x >> 16);

bb[index + 6] = (byte) (x >> 8);

bb[index + 7] = (byte) (x >> 0);

}


public static void putShort(byte b[], short s, int index) {

b[index] = (byte) (s >> 8);

b[index + 1] = (byte) (s >> 0);

}


public static short getShort(byte[] b, int index) {

return (short) (((b[index] << 8) | b[index + 1] & 0xff));

}



public static byte[] subArray(byte[] srcByte,int nStart, int nLength ){



byte[] ret = new byte[nLength];

System.arraycopy(srcByte, nStart, ret, 0, nLength);

return ret;

}



public static void main(String[] args){



byte[] b=new byte[3];

int year_offset=15; //2*2*2*2=16 > 16year[support:2011~2026] 00001011

int month=12; //2*2*2*2=16 > 12months 00000111

int day=31; //2*2*2*2*2=32 > 31days 00011111

int hour=24; //2*2*2*2*2=32 > 24hours 00010001

int min=59; //2*2*2*2*2*2=64 > 60minitues 00001001



// int year_offset=1; //2*2*2*2=16 > 16year[support:2011~2026] 00001011

// int month=1; //2*2*2*2=16 > 12months 00000111

// int day=1; //2*2*2*2*2=32 > 31days 00011111

// int hour=0; //2*2*2*2*2=32 > 24hours 00010001

// int min=0; //2*2*2*2*2*2=64 > 60minitues 00001001



//int temp=(int)(day<<11)+(int)(hour<<6)+(int)(min<<0);

int temp=(day<<11)+(hour<<6)+(min<<0);

System.out.println( "temp="+temp );

b[0] = (byte)((year_offset<<4)|month);

b[1] = (byte)(temp >> 8);

b[2] = (byte)(temp >> 0);



int temp2=(int) (

((((int)b[1])&0xFF) << 8) |

(((int)b[2])&0xFF));



System.out.println( "temp2="+temp2 );

int year_offset_reverse=(byte)((b[0] >> 4)& 0x0F);

int month_reverse=(byte)(b[0] & 0x0F);

int day_reverse=(byte)(temp2>>11);

//0000 0 111 11 00 0000 0x

int hour_reverse=(byte)((byte)((temp2 & 0x07C0) >>6));

//00000 00000 11111

//

int min_reverse=(byte)(((temp2 & 0x003F)));



System.out.println( "year_offset_reverse="+year_offset_reverse );

System.out.println( "month_reverse="+month_reverse );

System.out.println( "day_reverse="+day_reverse );

System.out.println( "hour_reverse="+hour_reverse );

System.out.println( "min_reverse="+min_reverse );



/*

b[0] = (byte)((year_offset<<4)|month);

//day-hour-min :2bytes :DAY[4BIT]+HOUR[low-4-BIT] - HOUR[hign-2-BIT]+MIN[6BIT]



b[1] = (byte)((day << 3)|hour << 4);

b[2] = (byte)(((min << 2))|hour>> 4);





char year_offset_reverse=(char)(b[0] >> 4);

char month_reverse=(char)(b[0] & 0x0F);

char day_reverse=(char)(b[1] >> 3);

char hour_reverse=(char)((b[2] << 6) | (b[1] & 0x0F));

char min_reverse=(char)(b[1] >> 2);



System.out.println( "year_offset_reverse="+year_offset_reverse );

System.out.println( "month_reverse="+month_reverse );

System.out.println( "day_reverse="+day_reverse );

System.out.println( "hour_reverse="+hour_reverse );

System.out.println( "min_reverse="+min_reverse );

*/





/*

b[0] = (byte)((year_offset<<4)|month);

//day-hour-min :2bytes :DAY[4BIT]+HOUR[low-4-BIT] - HOUR[hign-2-BIT]+MIN[6BIT]



b[1] = (byte)((day << 3)|hour << 4);

b[2] = (byte)(((min << 2))|hour>> 4);





char year_offset_reverse=(char)(b[0] >> 4);

char month_reverse=(char)(b[0] & 0x0F);

char day_reverse=(char)(b[1] >> 3);

char hour_reverse=(char)((b[2] << 6) | (b[1] & 0x0F));

char min_reverse=(char)(b[1] >> 2);



System.out.println( "year_offset_reverse="+year_offset_reverse );

System.out.println( "month_reverse="+month_reverse );

System.out.println( "day_reverse="+day_reverse );

System.out.println( "hour_reverse="+hour_reverse );

System.out.println( "min_reverse="+min_reverse );



*/









// short s=260;

//

// b[0] = (byte) (s >> 8);

// b[1] = (byte) (s >> 0);

// short s2=(short) (b[0] << 8);

// short SS=(short) (((b[0] << 8) | b[1] & 0xff));

//

// System.out.println(Long.MAX_VALUE);

// byte[] bs = long2bytes(Long.MAX_VALUE);

// System.out.println( bytes2long(bs) );

}

}

举报

相关推荐

0 条评论