#,#x,\u开头的unicode介绍
生活随笔
收集整理的這篇文章主要介紹了
#,#x,\u开头的unicode介绍
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
轉(zhuǎn)載地址:https://blog.csdn.net/u013243986/article/details/75287078
? ? ? ? 最近在寫爬蟲時遇到"&#"或者 "&#x"開頭的 編碼,在瀏覽器是可以正常打開的,但是爬取下來時卻,顯示??中国农业银行 在嘗試了??utf-8 或者GBK,GB2312等等的編碼都行不通的情況下, 在網(wǎng)上也找不到太多的資料,這讓我很苦惱,而且百度搜索居然沒法直接搜索符號,還是大谷歌好用,??通過http://tool.chinaz.com/tools/unicode.aspx??發(fā)現(xiàn)其實這個只是普通的unicode編碼.但是卻有不同的格式 比如\u開頭的,"&#"或者"&#x"開頭.??我們在?https://www.zhihu.com/question/21390312???找到了 問題的答案.??
?????????其實??\u 開頭和??&#x 開頭是一樣的??都是16進(jìn)制 unicode字符的不同寫法,&# 則是 unicode字符的10進(jìn)制的寫法.知道這個之后我們寫代碼就容易多了.
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介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux下c语言利用iconv函数实现
- 下一篇: UCS-2、UCS-4、UTF-8、UT