㈠ java中如何將unicode編碼的漢字串轉為gbk編碼
在一些應用場景,會出現這樣的需求:UTF-8 -> Unicode -> GBK,然而,Unicode與GBK沒有相對應的演算法可以直接轉換,前提:GBK與UNICODE沒有直接的對應關系,只能通過一張大表將兩者聯系起來。只能自己寫程序處理。
㈡ 用java如何把unicode碼轉成漢字
java中將unicode碼轉換成漢字的方式是直接使用string類型,列印即可:
Stringascii="u4f01u4e1a";//這兩個unicode碼就是企業的
System.out.println(ascii);//列印出來
運行結果:
企業
Unicode只有一個字元集,中、日、韓的三種文字佔用了Unicode中0x3000到0x9FFF的部分 Unicode目前普遍採用的是UCS-2,它用兩個位元組來編碼一個字元, 比如漢字"經"的編碼是0x7ECF,注意字元編碼一般用十六進制來 表示,為了與十進制區分,十六進制以0x開頭,0x7ECF轉換成十進制 就是32463,UCS-2用兩個位元組來編碼字元,兩個位元組就是16位二進制, 2的16次方等於65536,所以UCS-2最多能編碼65536個字元。
㈢ Java獲取字元的Unicode編碼以及如何過濾特
java中可以使用char類提供的charAt()方法來獲得字元的unicode的編碼值,示例如下:
public static String getUnicode(String source){ String returnUniCode=null; String uniCodeTemp=null; for(int i=0;i<source.length();i++){ uniCodeTemp = "\\u"+Integer.toHexString((int)source.charAt(i));//使用char類的charAt()的方法 returnUniCode=returnUniCode==null?uniCodeTemp:returnUniCode+uniCodeTemp; } System.out.print(source +" 's unicode = "+returnUniCode); return returnUniCode;//返回一個字元的unicode的編碼值}
㈣ java接收json數據沒有對Unicode解碼
/**
* 用於校驗一個字元串是否是合法的JSON格式
*
*/
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
public class JsonValidator {
private CharacterIterator it;
private char c;
private int col;
public JsonValidator(){
}
/**
* 驗證一個字元串是否是合法的JSON串
*
* @param input 要驗證的字元串
* @return true-合法 ,false-非法
*/
public boolean validate(String input) {
input = input.trim();
boolean ret = valid(input);
return ret;
}
private boolean valid(String input) {
if ("".equals(input)) return true;
boolean ret = true;
it = new StringCharacterIterator(input);
c = it.first();
col = 1;
if (!value()) {
ret = error("value", 1);
} else {
skipWhiteSpace();
if (c != CharacterIterator.DONE) {
ret = error("end", col);
}
}
return ret;
}
private boolean value() {
return literal("true") || literal("false") || literal("null") || string() || number() || object() || array();
}
private boolean literal(String text) {
CharacterIterator ci = new StringCharacterIterator(text);
char t = ci.first();
if (c != t) return false;
int start = col;
boolean ret = true;
for (t = ci.next(); t != CharacterIterator.DONE; t = ci.next()) {
if (t != nextCharacter()) {
ret = false;
break;
}
}
nextCharacter();
if (!ret) error("literal " + text, start);
return ret;
}
private boolean array() {
return aggregate('[', ']', false);
}
private boolean object() {
return aggregate('{', '}', true);
}
private boolean aggregate(char entryCharacter, char exitCharacter, boolean prefix) {
if (c != entryCharacter) return false;
nextCharacter();
skipWhiteSpace();
if (c == exitCharacter) {
nextCharacter();
return true;
}
for (;;) {
if (prefix) {
int start = col;
if (!string()) return error("string", start);
skipWhiteSpace();
if (c != ':') return error("colon", col);
nextCharacter();
skipWhiteSpace();
}
if (value()) {
skipWhiteSpace();
if (c == ',') {
nextCharacter();
} else if (c == exitCharacter) {
break;
} else {
return error("comma or " + exitCharacter, col);
}
} else {
return error("value", col);
}
skipWhiteSpace();
}
nextCharacter();
return true;
}
private boolean number() {
if (!Character.isDigit(c) && c != '-') return false;
int start = col;
if (c == '-') nextCharacter();
if (c == '0') {
nextCharacter();
} else if (Character.isDigit(c)) {
while (Character.isDigit(c))
nextCharacter();
} else {
return error("number", start);
}
if (c == '.') {
nextCharacter();
if (Character.isDigit(c)) {
while (Character.isDigit(c))
nextCharacter();
} else {
return error("number", start);
}
}
if (c == 'e' || c == 'E') {
nextCharacter();
if (c == '+' || c == '-') {
nextCharacter();
}
if (Character.isDigit(c)) {
while (Character.isDigit(c))
nextCharacter();
} else {
return error("number", start);
}
}
return true;
}
private boolean string() {
if (c != '"') return false;
int start = col;
boolean escaped = false;
for (nextCharacter(); c != CharacterIterator.DONE; nextCharacter()) {
if (!escaped && c == '\\') {
escaped = true;
} else if (escaped) {
if (!escape()) {
return false;
}
escaped = false;
} else if (c == '"') {
nextCharacter();
return true;
}
}
return error("quoted string", start);
}
private boolean escape() {
int start = col - 1;
if (" \\\"/bfnrtu".indexOf(c) < 0) {
return error("escape sequence \\\",\\\\,\\/,\\b,\\f,\\n,\\r,\\t or \\uxxxx ", start);
}
if (c == 'u') {
if (!ishex(nextCharacter()) || !ishex(nextCharacter()) || !ishex(nextCharacter())
|| !ishex(nextCharacter())) {
return error("unicode escape sequence \\uxxxx ", start);
}
}
return true;
}
private boolean ishex(char d) {
return "0123456789abcdefABCDEF".indexOf(c) >= 0;
}
private char nextCharacter() {
c = it.next();
++col;
return c;
}
private void skipWhiteSpace() {
while (Character.isWhitespace(c)) {
nextCharacter();
}
}
private boolean error(String type, int col) {
System.out.printf("type: %s, col: %s%s", type, col, System.getProperty("line.separator"));
return false;
}
public static void main(String[] args){
String jsonStr = "{\"website\":\"oschina.net\"}";
System.out.println(jsonStr+":"+new JsonValidator().validate(jsonStr));
}
}
㈤ java怎麼轉換為unicode編碼
public static String string2Unicode(String string) {
StringBuffer unicode = new StringBuffer();
for (int i = 0; i < string.length(); i++) {
// 取出每一個字元
char c = string.charAt(i);
// 轉換為unicode
unicode.append("\\u" + Integer.toHexString(c));
}
return unicode.toString();
}
㈥ java如何把以unicode編碼形式的字元串變成編碼前的形式
不用轉,直接輸出結果即可,系統會自動轉換。舉例:
System.out.println("u0061u0062u6c49u5b57");
結果就是:ab漢字。
㈦ java讀取含有unicode編碼的文件內容,並轉換成漢字
可以通過BufferedReader 流的形式進行流緩存,之後通過readLine方法獲取到緩存的內容。
BufferedReader bre = null;
try {
String file = "D:/test/test.txt";
bre = new BufferedReader(new FileReader(file));//此時獲取到的bre就是整個文件的緩存流
while ((str = bre.readLine())!= null) // 判斷最後一行不存在,為空結束循環
{
System.out.println(str);//原樣輸出讀到的內容(unicode會自動轉換為中文的)
};
備註:unicode不需要轉換的,直接輸出即可,會自動變成中文,如:
System.out.println("\u0061\u0062\u6c49\u5b57");
結果就是:ab漢字。
㈧ java'好' unicode編碼值是多少
是u597d
以下是用java轉化得到unicode的代碼,可以轉化字元串。
publicclassTest{
publicstaticvoidmain(Stringargs[]){
Stringindex="好";
for(inti=0;i<index.length();i++){
intu=(int)index.charAt(0);
Stringhex=Integer.toHexString(u);
System.out.println("\u"+hex+" ");
}
System.out.println("u597d");
}
}
㈨ java中如何輸出字元變數的Unicode編碼值
java中可以使用char類提供的charAt()方法來獲得字元的unicode的編碼值,示例如下:
(9)unicode解碼java擴展閱讀:
Java是一門面向對象編程語言,不僅吸收了C++語言的各種優點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特徵。Java語言作為靜態面向對象編程語言的代表,極好地實現了面向對象理論,允許程序員以優雅的思維方式進行復雜的編程。
Java具有簡單性、面向對象、分布式、健壯性、安全性、平台獨立與可移植性、多線程、動態性等特點。Java可以編寫桌面應用程序、Web應用程序、分布式系統和嵌入式系統應用程序等。
參考資料:網路-java
㈩ java 自動處理unicode編碼
感覺除非是對方修改,不然沒法處理,反斜杠開頭的,應該是被識別成了8進制的數字了