歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 用Java語言將utf8編碼的漢字還原

用Java語言將utf8編碼的漢字還原

日期:2017/3/1 9:46:25   编辑:Linux編程

說明:本文介紹了如何用Java將utf-8編碼的漢字還原

在網頁中的JavaScript中的中文都是經過編碼了的,通過浏覽器的”查看網頁源代碼”只能看到類似\u4e2d\u56fd的編碼。下面記錄了用Java語言解碼的過程。

import java.io.UnsupportedEncodingException;

public class Utf8ToChinese {
public static void main(String[] args) {
String strUtf8 = "\u4e2d\u56fd\u4f01\u4e1a\u5bb6\u6742\u5fd7";
String strChinese = null;

try {
strChinese = new String(strUtf8.getBytes("UTF-8"), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();

strChinese = "decode error";
}

System.out.println(strChinese);
}
}

以上代碼將utf-8編碼解碼結果為:中國企業家。

Copyright © Linux教程網 All Rights Reserved