UTF-8转换GB2312
来源:互联网 发布:mysql newid函数 编辑:程序博客网 时间:2024/05/01 08:04
Java则采用了平台无关的UNICODE,Java从一个byte流中读取一个字符串时,将把平台相关的byte转变为平台无关的Unicode字符串。在输出时Java将把Unicode字符串转变为平台相关的byte流,如果某个Unicode字符在某个平台上不存在,将会输出一个'?'。
GB2312编码中一个汉字是16位的,而UTF8中一个汉字却是24位的。
如果发出页面采用了UTF-8的编码,怎样实现转换到gb2312?
参照下例:
test1.htm
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p><a href="test2.jsp?abc=A打打B">啊啊啊啊啊啊</a></p>
</body>
</html>
test2.jsp
<%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<%
//String tofield = new String(request.getParameter("abc").getBytes("UTF-8"),"gb2312");
String s = request.getParameter("abc");
StringBuffer sb = new StringBuffer();
for(int i=0; i<s.length(); i++) {
char c = s.charAt(i);
switch (c) {
case '+':
sb.append(' ');
break;
case '%':
try {
sb.append((char)Integer.parseInt(
s.substring(i+1,i+3),16));
}
catch (NumberFormatException e) {
throw new IllegalArgumentException();
}
i += 2;
break;
default:
sb.append(c);
break;
}
}
// Undo conversion to external encoding
String result = sb.toString();
byte[] inputBytes = result.getBytes("8859_1");
out.println(new String(inputBytes,"UTF-8"));
%>
</body>
</html>
- UTF-8转换GB2312
- GB2312 <-> UTF-8 转换
- gb2312 utf-8 相互转换
- UTF-8转换GB2312函数
- java UTF-8转换GB2312
- UTF-8 转换成 GB2312
- UTF-8转换GB2312函数
- 编码转换utf-8/gb2312
- UTF-8和GB2312转换
- ASP汉字转换UTF-8以及UTF-8转换GB2312
- ASP汉字转换UTF-8及UTF-8转换GB2312
- 字符集之间转换(UTF-8,UNICODE,Gb2312)
- 字符集之间转换(UTF-8,UNICODE,Gb2312)
- 关于GB2312与UTF-8的转换
- gb2312 utf-8判断及转换
- 字符集之间转换(UTF-8,UNICODE,Gb2312)
- 字符集之间转换(UTF-8,UNICODE,Gb2312)
- 字符集之间转换(UTF-8,UNICODE,Gb2312)
- Software architecture, software engineering, and Renaissance Jazz
- 网络MAC地址
- 岁末
- [转]常用ASP代码
- Windows 预安装环境配置文件-磁盘配置[DiskConfig]
- UTF-8转换GB2312
- [原创]书评:再读《Microsoft .NET IL汇编语言程序设计》
- ASP.net 验证码(C#)
- 开源项目
- 如何才算掌握Java(J2SE篇)
- 调用MSSQLSERVER的系统过程xp_cmdshell发生错误
- ERP荒唐個案(一)--料号问题(ZT)
- OGRE乱记二
- ERP荒唐個案(二)-保稅倉的悲劇(ZT)