您的位置:程序门 -> 移动平台 -> windows mobile



wml页面从数据库读出数据到文本框全是乱码


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


wml页面从数据库读出数据到文本框全是乱码
发表于:2008-02-25 18:26:29 楼主
页面文字正常,但文本框里的文字全是乱码,症状如下,急求药方,谢谢

标识: <input   name="remark"   emptyok="false"   size="30"   value=" <%=remark%> "   maxlength="50"/> <br/>

remark是从数据库里读取的中文.显示在文本框里就乱码了.如果是数字或英文就很正常.

表头设置是: <%@page   contenttype="text/vnd.wap.wml;charset=utf-8"%>
如果改为: <%@page   contenttype="text/vnd.wap.wml;charset=gb2312"%> 后,整个页面的文字就全乱码了.

打开数据库看到的文字全是正常中文字.为什么读出来显示在文本框就不正常了呢?
发表于:2008-02-25 23:13:481楼 得分:0

就是要將   gb2312   的內容轉換成   utf-8   的編碼的,或者就和輸出指定的編碼格式相對應的。
发表于:2008-02-26 12:49:332楼 得分:0
意思要把文本框要显示的文字由数据库里gb2312       的內容转换成       utf-8   的编码是吗?
表头已经设置为 <%@page       contenttype="text/vnd.wap.wml;charset=utf-8"%> 了,为什么表单上的文本框就不能自动根据表头指示自动编码呢?
如果要自己把数据库里的gb2312的文字转换为utf-8的要怎么转?

非常感谢指点!
发表于:2008-02-27 01:12:303楼 得分:0
use   the   encoding   class   to   convert   gb2312   bytes   to   unicode   string,   then   to   utf-8   bytes
发表于:2008-02-27 14:40:044楼 得分:0
gb2312的文字转换为utf-8这么转,代码如下:

cstring   cpostgoods::toutf8(cstring   sourcestr,cstring   targetstr)
{

//unicode   to   utf8
char   *ptargetdata   =   null;                                              
int   targetlen=widechartomultibyte(cp_utf8,0,(lpcwstr)sourcestr,-1,ptargetdata,0,null,null);                                      
ptargetdata   =   new   char[targetlen+1];                      
memset(ptargetdata,0,targetlen+1);                                  
widechartomultibyte(cp_utf8,0,(lpcwstr)sourcestr,-1,ptargetdata,targetlen,null,null);                                  
char   *data   =   ptargetdata;

//utf8codelen
int   unicodelen=multibytetowidechar(cp_utf8,0,data,strlen(data)+1,null,0);
unicodelen+=1;

        //   a   wide   char   string      
wchar_t   *wszwidecharstring   =   null;
wszwidecharstring   =   new   wchar_t[unicodelen];

        //   convert   the   multy   byte   string   to   wide   char   string
      ::multibytetowidechar(cp_acp,0,data,strlen(data)+1,wszwidecharstring,unicodelen);

        //   display   the   wide   char   string
        //afxmessagebox(wszwidecharstring);
//output
cstring   result;
result   =   wszwidecharstring;
//afxmessagebox(result);

//release
delete   []wszwidecharstring;  
wszwidecharstring   =   null;  
delete   []ptargetdata;  
ptargetdata   =   null;

return   result;
}
发表于:2008-02-27 14:45:495楼 得分:0
上面函数中的result就是你要的结果。原输出参数targetstr没有用上。

期望以上代码能帮助你。
发表于:2008-02-27 14:57:306楼 得分:0
试这个c#的看看有没有用
c# code
public string urlencode(string str) { if (str == null) { return null; } return urlencode(str, encoding.utf8); } public string urlencode(string str, encoding e) { if (str == null) { return null; } byte[] by = this.urlencodetobytes(str, e); return encoding.ascii.getstring(by, 0, by.length); } public byte[] urlencodetobytes(string str, encoding e) { if (str == null) { return null; } byte[] buffer1 = e.getbytes(str); return urlencodebytestobytesinternal(buffer1, 0, buffer1.length, false); } private byte[] urlencodebytestobytesinternal(byte[] bytes, int offset, int count, bool alwayscreatereturnvalue) { int num1 = 0; int num2 = 0; forint num3 = 0; num3 < count; num3++) { char ch1 =char)bytes[offset + num3]; if (ch1 == ' ') { num1++; } else if!issafe(ch1)) { num2++; } } if ((!alwayscreatereturnvalue && (num1 == 0)) && (num2 == 0)) { return bytes; } byte[] buffer1 = new byte[count + (num2 * 2)]; int num4 = 0; forint num5 = 0; num5 < count; num5++) { byte num6 = bytes[offset + num5]; char ch2 =char)num6; if (issafe(ch2)) { buffer1[num4++] = num6; } else if (ch2 == ' ') { buffer1[num4++] = 0x2b; } else { buffer1[num4++] = 0x25; buffer1[num4++] =byte)inttohex((num6 >> 4) & 15); buffer1[num4++] =byte)inttohex(num6 & 15); } } return buffer1; } private bool issafe(char ch) { if ((((ch < 'a') || (ch > 'z')) && ((ch < 'a') || (ch > 'z'))) && ((ch < '0') || (ch > '9'))) { char ch1 = ch; switch (ch1) { case '\'': case '': case ')': case '*': case '-': case '.': case '!': { break; } case '+': case ',': { goto label_0057; } default: { if (ch1 != '_') { goto label_0057; } break; } } } return true; label_0057: return false; } private char inttohex(int n) { if (n <= 9) { returnchar)((ushort)(n + 0x30)); } returnchar)((ushort)((n - 10) + 0x61)); }
发表于:2008-02-27 18:42:027楼 得分:0
感觉你的数据库不是gb2312的编码,或者是tomcat里面的编码没设,用的是默认的

如果数据库的编码,页面编码,服务器的编码一致的话,是不会出现乱码的
发表于:2008-02-28 16:53:438楼 得分:0
表头设置是:   <%@page       contenttype="text/vnd.wap.wml;charset=utf-8"%>
当中utf-8改为gb2312试试。


快速检索

最新资讯
热门点击