📄 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 + -