Java的16進制與字符串的相互轉換函數

/**
  * 將指定byte數組以16進制的形式打印到控制檯
  * @param hint String
  * @param b byte[]
  * @return void
  */
public static void printHexString(String hint, byte[] b) {
   System.out.print(hint);
   for (int i = 0; i < b.length; i++) {
     String hex = Integer.toHexString(b[i] & 0xFF);
     if (hex.length() == 1) {
       hex = '0' + hex;
     }
     System.out.print(hex.toUpperCase() + " ");
   }
   System.out.println("");
}
/**
  *
  * @param b byte[]
  * @return String
  */
public static String Bytes2HexString(byte[] b) {
   String ret = "";
   for (int i = 0; i < b.length; i++) {
     String hex = Integer.toHexString(b[i] & 0xFF);
     if (hex.length() == 1) {
       hex = '0' + hex;
     }
     ret += hex.toUpperCase();
   }
   return ret;
}
/**
  * 將兩個ASCII字符合成一個字節;
  * 如:"EF"--> 0xEF
  * @param src0 byte
  * @param src1 byte
  * @return byte
  */
public static byte uniteBytes(byte src0, byte src1) {
   byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
   _b0 = (byte)(_b0 << 4);
   byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
   byte ret = (byte)(_b0 ^ _b1);
   return ret;
}
/**
  * 將指定字符串src,以每兩個字符分割轉換爲16進制形式
  * 如:"2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF, 0xD9}
  * @param src String
  * @return byte[]
  */
public static byte[] HexString2Bytes(String src){
   byte[] ret = new byte[8];
   byte[] tmp = src.getBytes();
   for(int i=0; i<8; i++){
     ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]);
   }
   return ret;

}



-------------------------------------------------------------------------------------------------------------

public static String stringToHexString(String strPart) {
        String hexString = "";
        for (int i = 0; i < strPart.length(); i++) {
            int ch = (int) strPart.charAt(i);
            String strHex = Integer.toHexString(ch);
            hexString = hexString + strHex;
        }
        return hexString;
    }

private static String hexString="0123456789ABCDEF";

public static String encode(String str)
{
// 根據默認編碼獲取字節數組
byte[] bytes=str.getBytes();
StringBuilder sb=new StringBuilder(bytes.length*2);
// 將字節數組中每個字節拆解成2位16進制整數
for(int i=0;i<bytes.length;i++)
{
sb.append(hexString.charAt((bytes[i]&0xf0)>>4));
sb.append(hexString.charAt((bytes[i]&0x0f)>>0));
}
return sb.toString();
}


public static String decode(String bytes)
{
ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2);
// 將每2位16進制整數組裝成一個字節
for(int i=0;i<bytes.length();i+=2)
baos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1))));
return new String(baos.toByteArray());
}

private static byte uniteBytes(byte src0, byte src1) {
     byte _b0 = Byte.decode("0x" + new String(new byte[] {src0})).byteValue();
     _b0 = (byte) (_b0 << 4);
     byte _b1 = Byte.decode("0x" + new String(new byte[] {src1})).byteValue();
     byte ret = (byte) (_b0 | _b1);
     return ret;

public static byte[] HexString2Bytes(String src)
{
   byte[] ret = new byte[6];
   byte[] tmp = src.getBytes();
   for(int i=0; i<6; ++i )
   {
    ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]);
      }
   return ret;
}

 

 ====================================================================

java 格式化輸出十六進制數

   // 以16進制輸出文件內容, 每16個數換行一次
   for(int i = 0; i < nLen; i++)
   {
    if(i % 16 == 0)
     System.out.println();
    String strHex = new String();
    strHex = Integer.toHexString(chBuf[i]).toUpperCase();
    if(strHex.length() > 3)
     System.out.print(strHex.substring(6));
    else
     if(strHex.length() < 2)
      System.out.print("0" + strHex);
     else
      System.out.print(strHex);
    
    System.out.print(" ");
   }
 
輸出結果
-----------------------------------------------------------------------
FF D8 FF E0 00 10 4A 46 49 46 00 01 01 00 00 01
00 01 00 00 FF E1 00 E6 45 78 69 66 00 00 49 49
2A 00 08 00 00 00 05 00 12 01 03 00 01 00 00 00
01 00 00 00 31 01 02 00 1C 00 00 00 4A 00 00 00
32 01 02 00 14 00 00 00 66 00 00 00 13 02 03 00
01 00 00 00 01 00 00 00 69 87 04 00 01 00 00 00
7A 00 00 00
 
=================================
當我們把string字符串轉成byte[]後,要再轉成string 通過String.valueof()是實現不了的,只能new string(byte [])。
 
呵呵,我在使用java 3des數據加密的時候,byte轉string,存到文件,再取出string再轉byte還是不對的,所以new string(byte [])也不見得會得到正確結果的!!最後我是採用byte轉成16進制字符串,然後再將16進制字符串轉換成byte纔可以的.

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