jsp在wap中的应用,关于unicode,大家看看

Iong 2002-03-27 11:55:15
public class UnicodeString
{
String my;
public UnicodeString(String me) {
my = new String(me);
}
public static String getUnicodeOf(String str) {
if (str == null)
return null;
StringBuffer tr = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
int tmp = str.charAt(i);
if (tmp < 0x100) {
tr.append(str.charAt(i));
} else {
tr.append("&#x");
tr.append(Integer.toHexString(str.charAt(i)));
tr.append(";");
}
}
return new String(tr);
}
public String getUnicodeOfMe() {
if (my == null)
return null;
StringBuffer tr = new StringBuffer();
for (int i = 0; i < my.length(); i++) {
int tmp = my.charAt(i);
if (tmp < 0x100) {
tr.append(my.charAt(i));
} else {
tr.append("&#x");
tr.append(Integer.toHexString(my.charAt(i)));
tr.append(";");
}
}
return new String(tr);
}

public static void main(String args[]) {
if (args.length < 1) {
System.out.println("usage: java UnicodeString [args...]");
System.exit(0);
}
String t = UnicodeString.getUnicodeOf(args[0]);
System.out.print(t);
}
}
以上是我写的unicode转码方法
用java UnicodeString [中english文]命令输出: [中english文]

问题是:
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-WAPFORUM//DTA WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<%@page contentType="text/vnd.wap.wml"%>
<%@page import="UnicodeString"%>
<wml>
<card id="he" title="<%=UnicodeString.getUnicodeOf("欢迎")%>" newcontext="true">
<p>
<%=UnicodeString.getUnicodeOf("欢迎您的到来!")%>//1。我希望他能转化为下面的样子
<%="欢迎您的到来!"%>//2。这是我用命令行方式拷出来的
</p>
</card>
</wml>
这样的一个jsp在模拟器中输出的 1。是乱码,2。却是正确的
...全文
41 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cooleyes 2002-03-28
  • 打赏
  • 举报
回复
如果你不用charset=gb2313的话
那默认就是charset=utf8
那样,你的函数转化就是错的
Iong 2002-03-28
  • 打赏
  • 举报
回复
把jsp的第3行改为<%@page contentType="text/vnd.wap.wml;charset=gb2312"%>
就好了,不知为什么,繁体或别的字体将会有理解方面问题
snowline 2002-03-27
  • 打赏
  • 举报
回复
我的代码,没问题:
public static String gb2312ToUnicode(String s)
{
String s1="";
if(s!=null && !s.equals(""))
try
{
String s2 = new String(s.getBytes(),"gb2312");
s = s2;
int i = s.length();
for(int j = 0; j<i; j++)
{
char c = s.charAt(j);
s1 = s1 + "&#x" + Integer.toHexString(c)+";";
}
}catch(Exception e)
{
System.out.println("ConverString Exception: "+e);
}
return s1;
}

public static String charToHex(char c)
{
byte byte0 = (byte)(c >>> 8);
byte byte1 = (byte)(c & 0xff);
return byteToHex(byte0) + byteToHex(byte1);
}

public static String byteToHex(byte byte0)
{
char[] ac = {'0', '1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
char[] ac1= { ac[byte0 >> 4 & 0xf], ac[byte0 & 0xf]};
return new String(ac1);
}
Iong 2002-03-27
  • 打赏
  • 举报
回复
推一下

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧