1. 程式人生 > >byte[]陣列與十六進位制字串與字串的互相轉換

byte[]陣列與十六進位制字串與字串的互相轉換

字串轉換成十六進位制字串方法1:

  /**
     * 字串轉換成十六進位制字串
     */ 
    public static String str2HexStr(String str) { 
        char[] chars = "0123456789ABCDEF".toCharArray(); 
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes(); 
        int bit; 
        for (int i = 0; i < bs.length; i++) { 
            bit = (bs[i] & 0x0f0) >> 4; 
            sb.append(chars[bit]); 
            bit = bs[i] & 0x0f; 
            sb.append(chars[bit]); 
        } 
        return sb.toString(); 
    } 

十六進位制字串轉換成為陣列方法1:

 /**
   * 把16進位制字串轉換成位元組陣列
   * @param hexString
   * @return byte[]
   */
  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;
  }
 
 private static int toByte(char c) {
    byte b = (byte) "0123456789ABCDEF".indexOf(c);
    return b;
 }

陣列轉換成十六進位制字串方法1:

 /**
  * 陣列轉換成十六進位制字串
  * @param byte[]
  * @return HexString
  */
 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();
 }

byte[]陣列轉換成十六進位制字串方法2:

    /**
     * 陣列轉成十六進位制字串
     * @param byte[]
     * @return HexString
     */
    public static String toHexString1(byte[] b){
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < b.length; ++i){
            buffer.append(toHexString1(b[i]));
        }
        return buffer.toString();
    }
    public static String toHexString1(byte b){
        String s = Integer.toHexString(b & 0xFF);
        if (s.length() == 1){
            return "0" + s;
        }else{
            return s;
        }
    }

十六進位制字串轉換字串方法1:

 /**
  * 十六進位制字串轉換成字串
  * @param hexString
  * @return String
  */
    public static String hexStr2Str(String hexStr) { 

        String str = "0123456789ABCDEF"; 
        char[] hexs = hexStr.toCharArray(); 
        byte[] bytes = new byte[hexStr.length() / 2]; 
        int n; 
        for (int i = 0; i < bytes.length; i++) { 
            n = str.indexOf(hexs[2 * i]) * 16; 
            n += str.indexOf(hexs[2 * i + 1]); 
            bytes[i] = (byte) (n & 0xff); 
        } 
        return new String(bytes); 
    }

十六進位制字串轉換字串方法2:

    /**
     * 十六進位制字串轉換字串
     * @param HexString
     * @return String
     */
 public static String toStringHex(String s) {
  byte[] baKeyword = new byte[s.length() / 2];
  for (int i = 0; i < baKeyword.length; i++) {
   try {
    baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(
      i * 2, i * 2 + 2), 16));
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  try {
   s = new String(baKeyword, "utf-8");// UTF-16le:Not
  } catch (Exception e1) {
   e1.printStackTrace();
  }
  return s;
 }

方式三:

  private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

    // 返回形式為數字跟字串(16進位制)
    private static String byteToArrayString(byte bByte) {
        int iRet = bByte;
        // System.out.println("iRet="+iRet);
        if (iRet < 0) {
            iRet += 256;
        }
        int iD1 = iRet / 16;
        int iD2 = iRet % 16;
        return strDigits[iD1] + strDigits[iD2];
    }