jython 中文问题解决方案
时间:2005-06-17 来源:junsi
# -*- coding: gb2312 -*-
FileInfo = """
#====================================================================
#========Project: Jython Demo Gb2312
#========Module: --
#========Author: junsi
#========Create Date: 2005-06-14
#========Update Date:
#====================================================================
"""
import string
import java.lang.String as jstring
def Unicode2Gb2312( astr ):
# type("") equal type( u"a") in jython
tmpstr = jstring( astr )
rtnstr = jstring ( tmpstr.getBytes("ISO8859_1"), "GB2312" )
if tmpstr.length() != rtnstr.length():
return rtnstr
else:
return astr
#
if __name__ == "__main__" :
a = """
如果我们不能改变这个世界,
我们应该努力改变我们的生活.
如果我们不能改变我们的生活,
我们应该改变我们对生活的态度."""
ustr = """
xc8xe7xb9xfbxcexd2xc3xc7xb2xbbxc4xdcxb8xc4xb1xe4xd5xe2xb8xf6xcaxc0xbdxe7,
xcexd2xc3xc7xd3xa6xb8xc3xc5xacxc1xa6xb8xc4xb1xe4xcexd2xc3xc7xb5xc4xc9xfaxbbxee.
xc8xe7xb9xfbxcexd2xc3xc7xb2xbbxc4xdcxb8xc4xb1xe4xcexd2xc3xc7xb5xc4xc9xfaxbbxee,
xcexd2xc3xc7xd3xa6xb8xc3xb8xc4xb1xe4xcexd2xc3xc7xb6xd4xc9xfaxbbxeexb5xc4xccxacxb6xc8."""
print "Gb2312 coding string"
print a; print
print "Unicode2Gb2312 convert Gb2312 coding string"
print Unicode2Gb2312(a); print
print "unicod stirng"
print ustr; print
print "Unicode2Gb2312 convert unicod stirng"
print Unicode2Gb2312(ustr); print