JAVA 十六進制與字符串的轉換


public static String toHexString(int i)以十六進制的無符號整數形式返回一個整數參數的字符串表示形式。
如果參數為負,那么無符號整數值為參數加上 232;否則等於該參數。將該值轉換為十六進制(基數 16)的無前導 0 的 ASCII
數字字符串。如果無符號數的大小值為零,則用一個零字符 '0' ('/u0030')
表示它;否則,無符號數大小的表示形式中的第一個字符將不是零字符。用以下字符作為十六進制數字:
0123456789abcdef
這些字符的范圍是從 '/u0030' 到 '/u0039' 和從 '/u0061' 到 '/u0066'。如果希望得到大寫字母,可以在結果上調用
String.toUpperCase() 方法:
Integer.toHexString(n).toUpperCase()
參數:
i - 要轉換成字符串的整數。
返回:
用十六進制(基數 16)參數表示的無符號整數值的字符串表示形式。

  1 //轉化字符串為十六進制編碼  
  2 public static String toHexString(String s) {  
  3    String str = "";  
  4    for (int i = 0; i < s.length(); i++) {  
  5     int ch = (int) s.charAt(i);  
  6     String s4 = Integer.toHexString(ch);  
  7     str = str + s4;  
  8    }  
  9    return str;  
 10 }  
 11 // 轉化十六進制編碼為字符串  
 12 public static String toStringHex1(String s) {  
 13    byte[] baKeyword = new byte[s.length() / 2];  
 14    for (int i = 0; i < baKeyword.length; i++) {  
 15     try {  
 16      baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(  
 17        i * 2, i * 2 + 2), 16));  
 18     } catch (Exception e) {  
 19      e.printStackTrace();  
 20     }  
 21    }  
 22    try {  
 23     s = new String(baKeyword, "utf-8");// UTF-16le:Not  
 24    } catch (Exception e1) {  
 25     e1.printStackTrace();  
 26    }  
 27    return s;  
 28 }  
 29 // 轉化十六進制編碼為字符串  
 30 public static String toStringHex2(String s) {  
 31    byte[] baKeyword = new byte[s.length() / 2];  
 32    for (int i = 0; i < baKeyword.length; i++) {  
 33     try {  
 34      baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(  
 35        i * 2, i * 2 + 2), 16));  
 36     } catch (Exception e) {  
 37      e.printStackTrace();  
 38     }  
 39    }  
 40    try {  
 41     s = new String(baKeyword, "utf-8");// UTF-16le:Not  
 42    } catch (Exception e1) {  
 43     e1.printStackTrace();  
 44    }  
 45    return s;  
 46 }  
 47 public static void main(String[] args) {  
 48    System.out.println(encode("中文"));  
 49    System.out.println(decode(encode("中文")));  
 50 }  
 51 /* 
 52 * 16進制數字字符集 
 53 */  
 54 private static String hexString = "0123456789ABCDEF";  
 55 /* 
 56 * 將字符串編碼成16進制數字,適用於所有字符(包括中文) 
 57 */  
 58 public static String encode(String str) {  
 59    // 根據默認編碼獲取字節數組  
 60    byte[] bytes = str.getBytes();  
 61    StringBuilder sb = new StringBuilder(bytes.length * 2);  
 62    // 將字節數組中每個字節拆解成2位16進制整數  
 63    for (int i = 0; i < bytes.length; i++) {  
 64     sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));  
 65     sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));  
 66    }  
 67    return sb.toString();  
 68 }  
 69 /* 
 70 * 將16進制數字解碼成字符串,適用於所有字符(包括中文) 
 71 */  
 72 public static String decode(String bytes) {  
 73    ByteArrayOutputStream baos = new ByteArrayOutputStream(  
 74      bytes.length() / 2);  
 75    // 將每2位16進制整數組裝成一個字節  
 76    for (int i = 0; i < bytes.length(); i += 2)  
 77     baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString  
 78       .indexOf(bytes.charAt(i + 1))));  
 79    return new String(baos.toByteArray());  
 80 }  
 81 // 第二種方法:  
 82 // 將指定byte數組以16進制的形式打印到控制台  
 83 // 復制代碼 代碼如下:  
 84 public class Util {  
 85    public Util() {  
 86    }  
 87    /** 
 88    * 將指定byte數組以16進制的形式打印到控制台 
 89    * @param hint String 
 90    * @param b byte[] 
 91    * @return void 
 92    */  
 93    public static void printHexString(String hint, byte[] b) {  
 94     System.out.print(hint);  
 95     for (int i = 0; i < b.length; i++) {  
 96      String hex = Integer.toHexString(b[i] & 0xFF);  
 97      if (hex.length() == 1) {  
 98       hex = '0' + hex;  
 99      }  
100      System.out.print(hex.toUpperCase() + " ");  
101     }  
102     System.out.println("");  
103    }  
104    /** 
105    * @param b byte[] 
106    * @return String 
107    */  
108    public static String Bytes2HexString(byte[] b) {  
109     String ret = "";  
110     for (int i = 0; i < b.length; i++) {  
111      String hex = Integer.toHexString(b[i] & 0xFF);  
112      if (hex.length() == 1) {  
113       hex = '0' + hex;  
114      }  
115      ret += hex.toUpperCase();  
116     }  
117     return ret;  
118    }  
119    /** 
120    * 將兩個ASCII字符合成一個字節; 如:"EF"--> 0xEF 
121    * @param src0 byte 
122    * @param src1 byte 
123    * @return byte 
124    */  
125    public static byte uniteBytes(byte src0, byte src1) {  
126     byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))  
127       .byteValue();  
128     _b0 = (byte) (_b0 << 4);  
129     byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))  
130       .byteValue();  
131     byte ret = (byte) (_b0 ^ _b1);  
132     return ret;  
133    }  
134    /** 
135    * 將指定字符串src,以每兩個字符分割轉換為16進制形式 如:"2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF, 0xD9} 
136    * @param src String 
137    * @return byte[] 
138    */  
139    public static byte[] HexString2Bytes(String src) {  
140     byte[] ret = new byte[8];  
141     byte[] tmp = src.getBytes();  
142     for (int i = 0; i < 8; i++) {  
143      ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);  
144     }  
145     return ret;  
146    }  
147 }  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM