首 页技术文章资源下载WAP论坛用户中心繁體中文
设为首页
加入收藏
联系我们
您当前的位置:中国WAP技术门户网站 -> 技术文章 -> 手机编程 -> Symbian -> 程序开发 -> 文章内容 退出登录 用户管理
热门文章
· [转载] wap2.0技巧篇
· [转载] WAP 2.0简介
· WML语言基础(WAP建站...
· [转载] WAP/WEB网站建设
· JSP技术在WAP开发中...
· [图文] WML语言基础(WAP建站...
· WML语言基础(WAP建站...
· [转载] 《女性安全期测算》...
· [转载] 跟你详说WAP
· asp.net开发wap程序...
相关文章
· 手机中文码制问题的...
 
手机中文码制问题的一点理解
文章作者:未知 [ 收藏此页到365Fav ]
文章来源:OGDEV [ 收藏此页到365Key ]
发布时间:2005-12-19 2:12:20 减小字体
文章编辑:atck 增大字体



服务器到客户端:
------------------------------------------------------------------
下面代码是服务器端把字符写到Client端,经过gbEncoding()方法,所有的字符编码成:\uXXXX.
-----------------------------------------------------------------

代码:-------------------------------------------------------------
/**
* Write the String data
*
* @param out
* @param value
*/
public static void writeUnicode(final DataOutputStream out, final String value) throws ActionException {
try {
final String unicode = StringFormatter.gbEncoding( value );
final byte[] data = unicode.getBytes();
final int dataLength = data.length;

System.out.println( 'Data Length is: ' + dataLength );
System.out.println( 'Data is: ' + value );
out.writeInt( dataLength ); //先写出字符串的长度
out.write( data, 0, dataLength ); //然后写出转化后的字符串
} catch (IOException e) {
throw new ActionException( IMDefaultAction.class.getName(), e.getMessage() );
}
}

----------------------------------------------------------------------
以下代码是gbEncoding()方法,把双字节字符转换成\uXXXX,ASIIC码在前面补00。
----------------------------------------------------------------------
/**
* This method will encode the String to unicode.
*
* @param gbString
* @return
*/

代码:--------------------------------------------------------------------------------
public static String gbEncoding( final String gbString ) {
char[] utfBytes = gbString.toCharArray();
String unicodeBytes = '';
for( int byteIndex = 0; byteIndex < utfBytes.length; byteIndex ++ ) {
String hexB = Integer.toHexString( utfBytes[ byteIndex ] );
if( hexB.length() <= 2 ) {
hexB = '00' + hexB;
}
unicodeBytes = unicodeBytes + '\\u' + hexB;
}
System.out.println( 'unicodeBytes is: ' + unicodeBytes );
return unicodeBytes;
}
--------------------------------------------------------------------------------

----------------------------------------------------------------------
在客户端收到服务器的数据,先将其一个一个字符解码。双字节显示正常。
----------------------------------------------------------------------

代码:--------------------------------------------------------------------------------
/**
* This method will decode the String to a recognized String
* in ui.
* @param dataStr
* @return
*/
private StringBuffer decodeUnicode( final String dataStr ) {
int start = 0;
int end = 0;
final StringBuffer buffer = new StringBuffer();
while( start > -1 ) {
end = dataStr.indexOf( '\\u', start + 2 );
String charStr = '';
if( end == -1 ) {
charStr = dataStr.substring( start + 2, dataStr.length() );
} else {
charStr = dataStr.substring( start + 2, end);
}
char letter = (char) Integer.parseInt( charStr, 16 ); // 16进制parse整形字符串。
buffer.append( new Character( letter ).toString() );
start = end;
}
return buffer;
}
--------------------------------------------------------------------------------

----------------------------------------------------------------------
客户端到服务器:
----------------------------------------------------------------------
客户端使用下面方法把手机端的字符编码成ISO-8859-1,传给服务器。
----------------------------------------------------------------------
代码:--------------------------------------------------------------------------------
/**
* write the String data
* @param value
* @param outData
*/
private void writeSjis(DataOutputStream outData, String value) {
try {
byte[] data = null;
// data = ( value ).getBytes( 'UTF-8' );
data = ( value ).getBytes( 'ISO8859_1' );
outData.writeInt(data.length);
outData.write(data, 0, data.length);

System.out.println(' data.length: ' + data.length);
System.out.println(' data.value: ' + value);
} catch (Exception ex) {
System.out.println(' write error ');
ex.printStackTrace();
}
}
--------------------------------------------------------------------------------

----------------------------------------------------------------------
服务器端收到客户端字符流,是用下面方法将其转为UTF-8,以后的操作都是基于UTF-8编码。SQLServer可能会由于内吗不通有不同的变换,所以存取数据库是还要是具体的DB内码作相应的处理。
----------------------------------------------------------------------

代码:--------------------------------------------------------------------------------
/**
*
* @param iso
* @return
*/
public static String isoToUtf( final String iso ) {
String utfString = iso;
if( iso != null ) {
try {
utfString = new String( iso.getBytes( 'ISO-8859-1' ), 'UTF-8' );
} catch ( UnsupportedEncodingException e ) {
utfString = iso;
}
} else {
utfString = '';
}
return utfString;
}

只要手机支持unicode的gb2312编码,应该都可以显示正常。

4。至于某些手机的模拟器不支持中文(譬如nokia 60系列),那真的没有办法了,只有等待他的中文版本出来了 呵呵,:)
 

上一页  [1] [2] 

 
[] [返回上一页] [打 印] [收 藏]
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]

关于我们  |   版权声明  |   广告服务  |   帮助中心  |   联系我们  |   网站地图  |   友情链接 
Copyright © 2004-2005 Wapzj.Com. All Rights Reserved .
粤ICP备05000730号 在线人数: