2進制,16進制,BCD,ascii,序列化對象相互轉換

public final static char[] BToA = "0123456789abcdef".toCharArray() ; 

 

1、16進制字符串轉爲字節數組

/**
    * 把16進制字符串轉換成字節數組
    * @param hex
    * @return
    */
public static byte[] hexStringToByte(String hex) {
    int len = (hex.length() / 2);
    byte[] result = new byte[len];
    char[] achar = hex.toCharArray();
    for (int i = 0; i < len; i++) {
     int pos = i * 2;
     result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
    }
    return result;
}

 

2、字節數組轉換成16進制字符串

/**
    * 把字節數組轉換成16進制字符串
    * @param bArray
    * @return
    */
public static final String bytesToHexString(byte[] bArray) {
    StringBuffer sb = new StringBuffer(bArray.length);
    String sTemp;
    for (int i = 0; i < bArray.length; i++) {
     sTemp = Integer.toHexString(0xFF & bArray[i]);
     if (sTemp.length() < 2)
      sb.append(0);
     sb.append(sTemp.toUpperCase());
    }
    return sb.toString();
}

 

3、10進制串轉爲BCD碼

/**
    * @函數功能: 10進制串轉爲BCD碼
    * @輸入參數: 10進制串
    * @輸出結果: BCD碼
    */
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;
}

 

4、BCD碼轉爲10進制串(阿拉伯數字)

/**
    * @函數功能: BCD碼轉爲10進制串(阿拉伯數據)
    * @輸入參數: BCD碼
    * @輸出結果: 10進制串
    */
public static String bcd2Str(byte[] bytes){
    StringBuffer temp=new StringBuffer(bytes.length*2);

    for(int i=0;i<bytes.length;i++){
     temp.append((byte)((bytes[i]& 0xf0)>>>4));
     temp.append((byte)(bytes[i]& 0x0f));
    }
    return temp.toString().substring(0,1).equalsIgnoreCase("0")?temp.toString().substring(1):temp.toString();
}

 

5、整數到字節數組轉換

public static byte[] int2bytes(int n) {
  byte[] ab = new byte[4];
  ab[0] = (byte) (0xff & n);
  ab[1] = (byte) ((0xff00 & n) >> 8);
  ab[2] = (byte) ((0xff0000 & n) >> 16);
  ab[3] = (byte) ((0xff000000 & n) >> 24);
  return ab;
 }

 

6、字節數組到整數的轉換

public static int bytes2int(byte b[]) {
  int s = 0;
  s = ((((b[0] & 0xff) << 8 | (b[1] & 0xff)) << 8) | (b[2] & 0xff)) << 8
    | (b[3] & 0xff);
  return s;
 }

 

7、字節轉換到字符

public static char byte2char(byte b) {
  return (char) b;
 }

 

8、2進制字符串轉爲字節

    public static byte binaryToByte(String binaryStr){
        Integer i = Integer.valueOf(binaryStr,2);
        return i.byteValue();
    }

 

9、字節數組轉換爲對象

/**
    * 把字節數組轉換爲對象
    * @param bytes
    * @return
    * @throws IOException
    * @throws ClassNotFoundException
    */
public static final Object bytesToObject(byte[] bytes) throws IOException, ClassNotFoundException {
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
    ObjectInputStream oi = new ObjectInputStream(in);
    Object o = oi.readObject();
    oi.close();
    return o;
}

 

10、可序列化對象轉換成字節數組

/**
    * 把可序列化對象轉換成字節數組
    * @param s
    * @return
    * @throws IOException
    */
public static final byte[] objectToBytes(Serializable s) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ObjectOutputStream ot = new ObjectOutputStream(out);
    ot.writeObject(s);
    ot.flush();
    ot.close();
    return out.toByteArray();
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章