?? unicode.java
字號:
/*
* @(#)Unicode.java 1.11 01/08/23
* Copyright (c) 2004-2005 wuhua of workroom Inc. All Rights Reserved.
* @version 1.0, 10/05/2004
* @author 饒榮慶
* @author 余煜輝
*/
/**
*此common包是連網的共有包,為所有連網提供統一的接口與其他一些特殊處理類
*/
package com.j2me.common;
/**
*此類是用于處理手機程序訪問網絡時處理中文問題的公有類
*它有兩個靜態方法 stringToUnicode(String s), unicodeToString(String s)
* stringToUnicode(String s)用于處理把其他具體字符(中文,英文)轉換為Unicode字符
* unicodeToString(String s)則用于處理把Unicode字符轉換為其他具體字符(中文,英文)
*/
public class Unicode
{
/*將字符轉為Unicode*/
public static String stringToUnicode(String s)
{
if (s == null)
{
return null;
}
StringBuffer result = new StringBuffer();
int i;
for (i = 0; i < s.length(); i++ )
{
if (s.charAt(i) >= 0x2018) //漢字Unicode的編碼都大于0x2018,只需要將漢字轉換成Unicode
{
result.append('\\'); //加上表示Unicode的
result.append('u');
String hex = Integer.toHexString(s.charAt(i)); //將對應字符轉換成16進制表示(因為Unicode是16進制數表示)
result.append(hex);
}
else if (s.charAt(i) == 0x005c)
{
result.append("\\u005c");
}
else //英文的就不用轉換了
{
result.append(s.charAt(i));
}
}
return result.toString();
}
/*將Unicode轉為字符*/
public static String unicodeToString(String s)
{
if (s == null)
{
return null;
}
StringBuffer result = new StringBuffer();
int tempI, i, j, ch;
for (i = 0; i < s.length(); i++ )
{
if ((ch = s.charAt(i)) == '\\') //如果是Unicode編碼(開始是),則將它轉換為漢字
{
tempI = i;
i+=2;
while (s.length() > i && s.charAt(i) == 'u')
{
i++;
}
if (s.length() >= i + 4)
{
ch = Integer.parseInt(s.substring(i, i+4), 16); //將Unicode16進制數轉換為10進制
i+=3;
}
else
{
i = tempI;
}
}
//對于漢字,將它從整形數據轉換成字符后,附在result后,對于英文字符,直接使用就行
result.append((char)ch);
}
return result.toString();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -