java編碼unicode編碼
『壹』 在java中,字元使用的是不是16位的Unicode編碼,這是對還是錯
是這樣的:
java中的字元就是指char類型的變數,無論中文還是英文,都是佔2個位元組,因為都是用Unicode編碼,一個Unicode編碼就是16位,也就是2個位元組。
所以
char
a='中';
char
b='e';
都是合法的
substring
處理的最小單元就是一個字元,也就是一個char類型,2個位元組
『貳』 java中Unicode碼的轉換
private static String decode(char[] in) throws Exception {
int off = 0;
char c;
char[] out = new char[in.length];
int outLen = 0;
while (off < in.length) {
c = in[off++];
if (c == '\\') {
if (in.length > off) { // 是否有下一個字元
c = in[off++]; // 取出下一個字元
} else {
out[outLen++] = '\\'; // 末字元為'\',返回
break;
}
if (c == 'u') { // 如果是"\\u"
int value = 0;
if (in.length > off + 4) { // 判斷"\\u"後邊是否有四個字元
boolean isUnicode = true;
for (int i = 0; i < 4; i++) { // 遍歷四個字元
c = in[off++];
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = (value << 4) + c - '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
value = (value << 4) + 10 + c - 'a';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
value = (value << 4) + 10 + c - 'A';
break;
default:
isUnicode = false; // 判斷是否為unicode碼
}
}
if (isUnicode) { // 是unicode碼轉換為字元
out[outLen++] = (char) value;
} else { // 不是unicode碼把"\\uXXXX"填入返回值
off = off - 4;
out[outLen++] = '\\';
out[outLen++] = 'u';
out[outLen++] = in[off++];
}
} else { // 不夠四個字元則把"\\u"放入返回結果並繼續
out[outLen++] = '\\';
out[outLen++] = 'u';
continue;
}
} else {
switch (c) { // 判斷"\\"後邊是否接特殊字元,回車,tab一類的
case 't':
c = '\t';
out[outLen++] = c;
break;
case 'r':
c = '\r';
out[outLen++] = c;
break;
case 'n':
c = '\n';
out[outLen++] = c;
break;
case 'f':
c = '\f';
out[outLen++] = c;
break;
default:
out[outLen++] = '\\';
out[outLen++] = c;
break;
}
}
} else {
out[outLen++] = (char) c;
}
}
return new String(out, 0, outLen);
}
『叄』 java中怎麼獲得字元的Unicode碼
//程序里的getBytes方法可以實現你要的功能,你看看。
//你運行這個程序了么,我運行的時候都好著啊。
//你也可以變通一下啊,看裡面的來龍去脈都是什麼樣的哦,
//那樣就知道怎麼弄了,而不是依靠別人。這個不是很難。
//out.append(str);
//out.append(str1); 這兩句千萬不能顛倒。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class Unicode
{
public static void main(String[] args)
{
Unicode instance = new Unicode();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line;
try
{
while ((line = reader.readLine()) != null)
{
if(line.trim().equals("q")) System.exit(0);
String s = instance.getBytes(line);
System.out.println("bytes:" + s);
//System.out.println("line:"+);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
String getBytes(String s)
{
try
{
StringBuffer out = new StringBuffer("");
byte[] bytes = s.getBytes("unicode");
for (int i = 2; i < bytes.length-1; i+=2)
{
out.append("\\u");
String str = Integer.toHexString(bytes[i+1] & 0xff);
for (int j = str.length(); j < 2; j++)
{
out.append("0");
}
String str1 = Integer.toHexString(bytes[i] & 0xff);
out.append(str);
out.append(str1);
}
return out.toString();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
return null;
}
}
}
『肆』 JAVA編程 Unicode碼是什麼
世界上所有文字 都能用unicode表示,這樣就沒有國界,沒有機器的差別了。
每個字元有一個相應的unicode碼,在java中可以這樣定義一個String對象:
String s = new String("\u4ed6");// \u4ed6就是一個unicode碼,就是一個字元,你可以System.out.print("\u4ed6");//試試
http://ke..com/view/40801.htm
『伍』 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編碼代表的字元
publicclassCharT{
publicstaticvoidmain(String[]args){//toChars(intcodePoint)
Stringstr="u554A";//啊字的unicode
intcodePoint=Integer.parseInt("554A",16);
char[]chs=Character.toChars(codePoint);
System.out.println(chs[0]);
}
}
應該是你要的答案吧...
『柒』 java中如何輸出字元變數的Unicode編碼值
java中可以使用char類提供的charAt()方法來獲得字元的unicode的編碼值,示例如下:
(7)java編碼unicode編碼擴展閱讀:
Java是一門面向對象編程語言,不僅吸收了C++語言的各種優點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特徵。Java語言作為靜態面向對象編程語言的代表,極好地實現了面向對象理論,允許程序員以優雅的思維方式進行復雜的編程。
Java具有簡單性、面向對象、分布式、健壯性、安全性、平台獨立與可移植性、多線程、動態性等特點。Java可以編寫桌面應用程序、Web應用程序、分布式系統和嵌入式系統應用程序等。
參考資料:網路-java
『捌』 編寫JAVA程序輸出中文字的unicode編碼
我寫的,你試試,你可以把它改寫成循環的,可以一直把字元的Unicode輸出,完善後發給我哈:
import java.io.*;
public class FindUnicode {
public static void main(String[] args) throws IOException{
InputStreamReader read = new InputStreamReader (System.in);
int ch = read.read();
System.out.print("\\u"+Integer.toHexString(ch));
read.close();
}
}
『玖』 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編碼
感覺除非是對方修改,不然沒法處理,反斜杠開頭的,應該是被識別成了8進制的數字了