utf8 and unicode
生活随笔
收集整理的這篇文章主要介紹了
utf8 and unicode
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
使用如下代碼得到漢字“年”的unicdoe 編碼和UTF8編碼:
package test;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class Test {public static String getUTF8EnCodeFromText(String text) {StringBuffer sb = new StringBuffer(); sb.append(text); String xmString = ""; String xmlUTF8 = ""; try { xmString = new String(sb.toString().getBytes("UTF-8")); xmlUTF8 = URLEncoder.encode(xmString, "UTF-8"); System.out.println("UTF8 Code:" + xmlUTF8) ; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return xmlUTF8;} public static String getUniCode(char single) {StringBuffer output = new StringBuffer();output.append(Integer.toString(single, 16));System.out.println("Unicode: " + output);return output.toString();}public static void main(String[] args) throws UnsupportedEncodingException {Test.getUTF8EnCodeFromText("2014年12月1日和聯(lián)想有一個(gè)重要的銷售會(huì)議");Test.getUniCode('年');Test.getUTF8EnCodeFromText("年");char ab = ((char)Integer.parseInt("5e74", 16)); System.out.println("original character: " + ab);}
}
輸出:
如果用記事本打開一個(gè)具有如下內(nèi)容的txt文件:
用hex editor打開,發(fā)現(xiàn)該字符的編碼為 C4 EA
通過檢查發(fā)現(xiàn)該txt file的保存方式是ANSI:
改成utf8后,再用hex eidtor打開就能觀察到期望的編碼如下:
通過訪問網(wǎng)站:?http://www.ab173.com/utf8.php能得到如何用javascript 進(jìn)行轉(zhuǎn)換的source code:
總結(jié)
以上是生活随笔為你收集整理的utf8 and unicode的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Error starting stati
- 下一篇: 海康、大华IPC的rtsp格式