#,#x,\u开头的unicode介绍
轉(zhuǎn)載地址:https://blog.csdn.net/u013243986/article/details/75287078
? ? ? ? 最近在寫爬蟲時(shí)遇到"&#"或者 "&#x"開頭的 編碼,在瀏覽器是可以正常打開的,但是爬取下來(lái)時(shí)卻,顯示??中国农业银行 在嘗試了??utf-8 或者GBK,GB2312等等的編碼都行不通的情況下, 在網(wǎng)上也找不到太多的資料,這讓我很苦惱,而且百度搜索居然沒法直接搜索符號(hào),還是大谷歌好用,??通過(guò)http://tool.chinaz.com/tools/unicode.aspx??發(fā)現(xiàn)其實(shí)這個(gè)只是普通的unicode編碼.但是卻有不同的格式 比如\u開頭的,"&#"或者"&#x"開頭.??我們?cè)?https://www.zhihu.com/question/21390312???找到了 問(wèn)題的答案.??
?????????其實(shí)??\u 開頭和??&#x 開頭是一樣的??都是16進(jìn)制 unicode字符的不同寫法,&# 則是 unicode字符的10進(jìn)制的寫法.知道這個(gè)之后我們寫代碼就容易多了.
public static String unicode2String(String unicode) {StringBuffer string = new StringBuffer();if (unicode.startsWith("&#x")) {String[] hex = unicode.replace("&#x", "").split(";");for (int i=0; i<hex.length; i++) {int data = Integer.parseInt(hex[i], 16);string.append((char) data);}} else if (unicode.startsWith("&#")) {String[] hex = unicode.replace("&#", "").split(";");for (int i=0; i<hex.length; i++) {int data = Integer.parseInt(hex[i], 10);string.append((char) data);}}return string.toString(); }?
總結(jié)
以上是生活随笔為你收集整理的#,#x,\u开头的unicode介绍的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux下c语言利用iconv函数实现
- 下一篇: UCS-2、UCS-4、UTF-8、UT