參考網址http://www.oschina.net/code/snippet_142385_4297java
http://canofy.iteye.com/blog/718659git
在java的不少配置文件中,尤爲是國際化資源中常常遇到相似\uf432這樣的unicode編碼,蒐集了下該編碼相關的資料,大體處理方法有以下:app
一、Unicode轉 漢字字符串。函數
這個過程最簡單的方式就是直接獲取。好比學習
String cnStr = "\ufeff\u4e2d\u56fd\u4eba";編碼
System.out.println(cnStr); 便可獲取對應的漢字字符 「中國人」;.net
可是呢,每次從輸出讀的話也未免過於不方便了,咱們使用方法來作轉換,直接獲取。code
參考以下blog
public static String unicodeToString(String str) { Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))"); Matcher matcher = pattern.matcher(str); char ch; while (matcher.find()) { ch = (char) Integer.parseInt(matcher.group(2), 16); str = str.replace(matcher.group(1), ch + ""); } return str; }
二、獲取字符串的unicode編碼,這個咱們能夠經過直接獲取字符串的unicode二進制,而後將其byte轉換成對應的16進製表示便可,函數示例以下ip
static String getUnicode(String s) { try { StringBuffer out = new StringBuffer(""); byte[] bytes = s.getBytes("unicode"); for (int i = 0; i < bytes.length - 1; i += 2) { out.append("\\u"); String str = Integer.toHexString(bytes[i + 1] & 0xff); for (int j = str.length(); j < 2; j++) { out.append("0"); } String str1 = Integer.toHexString(bytes[i] & 0xff); out.append(str1); out.append(str); } return out.toString(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } }
經過上面的方式即可完整的使用unicode編碼了,你們有其餘方式的轉換也能夠告訴我下,互相學習