2008年05月的归档
今天早上,网页忽然正常显示了,原来是修改了注册表的原因。
在数据库迁移后(sql server :gbk2312 到 Oracle : UTF8),可能出现页面字符乱码的原因:
1.oracle字符集设置不正确。
2.迁移过程中字符编码转换不正确,如何转换编码可参照总结1。
在php中,可以使用mb_string 的mb_detect_encoding来检测字符串是什么编码。
3.脚本代码要另存为utf8格式。
4.页面的header 要设置为utf8。
在数据库迁移后(sql server :gbk2312 到 Oracle : UTF8),可能出现页面字符乱码的原因:
1.oracle字符集设置不正确。
数据库服务器字符集select * from nls_database_parameters,其来源于props$,是表示数据库的字符集。
客户端字符集环境select * from nls_instance_parameters,其来源于v$parameter,表示客户端的字符集的设置。
可能是参数文件,环境变量或者是注册表会话字符集环境select * from nls_session_parameters,
其来源于v$nls_parameters,表示会话自己的设置,可能是会话的环境变量或者是alter session完成,
如果会话没有特殊的设置,将与nls_instance_parameters一致。
客户端的字符集要求与服务器一致,才能正确显示数据库的非Ascii字符。如果多个设置存在的时候,
alter session>环境变量>注册表>参数文件字符集要求一致,但是语言设置却可以不同,语言设置建议用英文。
如字符集是zhs16gbk,则nls_lang可以是American_America.zhs16gbk;如果字符集是utf8,则要改成
SIMPLIFIED CHINESE_CHINA.AL32UTF8。
(即hkey_local_machine => software => oracle =>NLS_LANG改为SIMPLIFIED CHINESE_CHINA.AL32UTF8)
2.迁移过程中字符编码转换不正确,如何转换编码可参照总结1。
在php中,可以使用mb_string 的mb_detect_encoding来检测字符串是什么编码。
3.脚本代码要另存为utf8格式。
4.页面的header 要设置为utf8。
下载地址:http://createwebapp.com/
文档地址:http://createwebapp.com/autocomplete 文档非常详细。
需要注意的一点是
<script>
new Autocomplete("consumerName", function() {
return "consumers.php?q=" + this.value;
});
</script>
这段js要写在
之后。
截图如下:
文档地址:http://createwebapp.com/autocomplete 文档非常详细。
需要注意的一点是
<script>
new Autocomplete("consumerName", function() {
return "consumers.php?q=" + this.value;
});
</script>
这段js要写在
之后。
截图如下:
基础:
public String changeCharset(String str)
throws UnsupportedEncodingException {
if (str != null) {
//得到gbk编码的字节串
byte[] bs = str.getBytes("GBK");
System.out.println("gbk:");
System.out.println("hex format is:"+encodeHex(bs));
//得到utf8编码的字节串
byte[] ns = str.getBytes("UTF-8");
System.out.println("utf8:");
System.out.println("hex format is:"+encodeHex(ns));
System.out.println("Bin format is:"+encodeBin(ns)
);
//将编码为utf8的字节串再生成新的字符串,然后解码,看看转换过程中是否出错
String newutf=new String(ns,"UTF-8");
System.out.println("convert back from utf8:"+encodeHex(newutf.getBytes("UTF-8")));
return newutf;
}
public static final String encodeHex (byte[] bytes)
{
StringBuffer buff =
new StringBuffer(bytes.length * 2);
String b;
for (int i=0; i<bytes.length ; i++)
{
b = Integer.toHexString(bytes[i]);
// byte是两个字节的, 而上面的Integer.toHexString会把字节扩展为4个字节
buff.append(b.length() > 2 ? b.substring(6,8) : b);
buff.append(" ");
}
return buff.toString();
}
public static final String encodeBin (byte[] bytes)
{
StringBuffer buff =
new StringBuffer(bytes.length * 2);
String b;
for (int i=0; i<bytes.length ; i++)
{
b = Integer.toBinaryString(bytes[i]);
// byte是两个字节的, 而上面的Integer.toHexString会把字节扩展为4个字节
buff.append(b.substring(24,b.length()));
buff.append(" ");
}
return buff.toString();
}
在Java中,String.getBytes("GBK")获得的是gbk编码的字节串,打印出来之后得到的是对应编码的十进制值,转换成16进制后就和编码表中的值一样了。
最后可以根据输出的二进制编码,和unicode表对比。
示例:
str: 中国
gbk:
hex format is:d6 d0 b9 fa
utf8:
hex format is:e4 b8 ad e5 9b bd
Bin format is:11100100 10111000 10101101 11100101 10011011 10111101
convert back from utf8:e4 b8 ad e5 9b bd
其中,“中”的unicode编码是4e2d,二进制表示为0100 1110 0010 1101,从低位到高位按六位截取及补齐高位后就可以得到11100100 10111000 1010110 ,即为utf8编码。
中文编码基础知识介绍(理论基础)
Java 编程技术中汉字问题的分析及解决
String.getBytes()方法中的中文编码问题 (这篇最为简单易懂且结合实际)
Unicode和UTF-8之间的转换详解
附录:
unicode编码表
GBK 汉字内码扩展规范编码表(二)
代码示例:public String changeCharset(String str)
throws UnsupportedEncodingException {
if (str != null) {
//得到gbk编码的字节串
byte[] bs = str.getBytes("GBK");
System.out.println("gbk:");
System.out.println("hex format is:"+encodeHex(bs));
//得到utf8编码的字节串
byte[] ns = str.getBytes("UTF-8");
System.out.println("utf8:");
System.out.println("hex format is:"+encodeHex(ns));
System.out.println("Bin format is:"+encodeBin(ns)
);
//将编码为utf8的字节串再生成新的字符串,然后解码,看看转换过程中是否出错
String newutf=new String(ns,"UTF-8");
System.out.println("convert back from utf8:"+encodeHex(newutf.getBytes("UTF-8")));
return newutf;
}
public static final String encodeHex (byte[] bytes)
{
StringBuffer buff =
new StringBuffer(bytes.length * 2);
String b;
for (int i=0; i<bytes.length ; i++)
{
b = Integer.toHexString(bytes[i]);
// byte是两个字节的, 而上面的Integer.toHexString会把字节扩展为4个字节
buff.append(b.length() > 2 ? b.substring(6,8) : b);
buff.append(" ");
}
return buff.toString();
}
public static final String encodeBin (byte[] bytes)
{
StringBuffer buff =
new StringBuffer(bytes.length * 2);
String b;
for (int i=0; i<bytes.length ; i++)
{
b = Integer.toBinaryString(bytes[i]);
// byte是两个字节的, 而上面的Integer.toHexString会把字节扩展为4个字节
buff.append(b.substring(24,b.length()));
buff.append(" ");
}
return buff.toString();
}
在Java中,String.getBytes("GBK")获得的是gbk编码的字节串,打印出来之后得到的是对应编码的十进制值,转换成16进制后就和编码表中的值一样了。
最后可以根据输出的二进制编码,和unicode表对比。
示例:
str: 中国
gbk:
hex format is:d6 d0 b9 fa
utf8:
hex format is:e4 b8 ad e5 9b bd
Bin format is:11100100 10111000 10101101 11100101 10011011 10111101
convert back from utf8:e4 b8 ad e5 9b bd
其中,“中”的unicode编码是4e2d,二进制表示为0100 1110 0010 1101,从低位到高位按六位截取及补齐高位后就可以得到11100100 10111000 1010110 ,即为utf8编码。
