/**
* 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;
}

/**
* 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;
   int temp = 0;
   int 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[] str2Bcd(String asc) { 
    int len = asc.length(); 
    int mod = len % 2; 
    if (mod != 0) { 
     asc = "0" + asc; 
     len = asc.length(); 
    } 
    byte abt[] = new byte[len]; 
    if (len >= 2) { 
     len = len / 2; 
    } 
    byte bbt[] = new byte[len]; 
    abt = asc.getBytes(); 
    int j, k; 
    for (int p = 0; p < asc.length()/2; p++) { 
     if ( (abt[2 * p] >= '0') && (abt[2 * p] <= '9')) { 
      j = abt[2 * p] - '0'; 
     } else if ( (abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) { 
      j = abt[2 * p] - 'a' + 0x0a; 
     } else { 
      j = abt[2 * p] - 'A' + 0x0a; 
     } 
     if ( (abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) { 
      k = abt[2 * p + 1] - '0'; 
     } else if ( (abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) { 
      k = abt[2 * p + 1] - 'a' + 0x0a; 
     }else { 
      k = abt[2 * p + 1] - 'A' + 0x0a; 
     } 
     int a = (j << 4) + k; 
     byte b = (byte) a; 
     bbt[p] = b; 
    }     return bbt; 
}
posted on 2011-10-14 14:01  daiqing  阅读(208)  评论(0)    收藏  举报