我搜索了一下帖子,发现大多数人都有utf-8的问题,并且一般都是在web中碰到的,其作法就是改charset,可是我的程序与web无关,我抓取内容后,在textarea中显示,我尝试
content = new String(content.getBytes("UTF-8"), "GB2312");
可是出来的却是乱码,后来我做了一个测试,发现utf-8不能转换为gb2312
String ttt = "我是中文";
ttt = new String(ttt.getBytes("GB2312"), "UTF-8"); //ttt = ��������
ttt = new String(ttt.getBytes("UTF-8"), "GB2312"); //ttt = 锟斤拷锟斤拷锟斤拷锟斤拷
最后“我是中文”这几个汉字就变成了“锟斤拷锟斤拷锟斤拷锟斤拷”这种乱七八糟的东西了,请问,utf-8要转换成gb2312该怎么解决?
11 个解决方案
#1
String ttt = "我是中文";这行中的字符是Unicode编码
#2
那现在这么问吧,我从文件中获得了一些字符串,这些字符串是采用的utf-8编码,我现在想把他转化成人们可读的gb2312怎么做?谢谢了!
#3
假设String str;//为utf-8
new String(str.getBytes(),"utf-8");这样解码应该久可以了
new String(str.getBytes(),"utf-8");这样解码应该久可以了
#4
假设String str;//为utf-8
new String(str.getBytes("GBK),"utf-8");
new String(str.getBytes("GBK),"utf-8");
#5
为了让大家可以测试,也避免浪费大家宝贵的时间,你们可以做一下测试
test.java
public class test {
public static void main(String args[]) {
String ttt = "我是中文";
try {
ttt = new String(ttt.getBytes(), "UTF-8"); //这时ttt是utf-8编码大家应该没有异议吧?
System.out.println("ttt = " + ttt);
ttt = new String(ttt.getBytes("GBK"), "UTF-8"); //这里怎么写才能让ttt变为“我是中文”
System.out.println("ttt = " + ttt);
}
catch (Exception e) {}
}
}
test.java
public class test {
public static void main(String args[]) {
String ttt = "我是中文";
try {
ttt = new String(ttt.getBytes(), "UTF-8"); //这时ttt是utf-8编码大家应该没有异议吧?
System.out.println("ttt = " + ttt);
ttt = new String(ttt.getBytes("GBK"), "UTF-8"); //这里怎么写才能让ttt变为“我是中文”
System.out.println("ttt = " + ttt);
}
catch (Exception e) {}
}
}
#6
package com.boasoft;
class Swap
{
public static void main(String args[])
{
StringBuffer buffer = new StringBuffer("這邊這個東東");
StringBuffer tmp = new StringBuffer();
int prePos = 0;
int pos = 0;
while ( (pos = buffer.indexOf("&#")) >= 0 )
{
prePos = pos;
pos = buffer.indexOf(";", prePos);
if ( pos >= 0 && pos > (prePos+2) )
{
tmp.setLength(0);
tmp.append ( (char)Integer.parseInt(buffer.substring(prePos+2, pos)) );
buffer.delete (prePos, pos+1);
buffer.insert(prePos, tmp.toString());
}
}
System.out.println(buffer.toString());
}
}
你说的是汉字(HZ)编码格式(一种使用ASCII码来表示汉字的方法,多在海外华人中使用)
,不是UTF-8的编码格式。
class Swap
{
public static void main(String args[])
{
StringBuffer buffer = new StringBuffer("這邊這個東東");
StringBuffer tmp = new StringBuffer();
int prePos = 0;
int pos = 0;
while ( (pos = buffer.indexOf("&#")) >= 0 )
{
prePos = pos;
pos = buffer.indexOf(";", prePos);
if ( pos >= 0 && pos > (prePos+2) )
{
tmp.setLength(0);
tmp.append ( (char)Integer.parseInt(buffer.substring(prePos+2, pos)) );
buffer.delete (prePos, pos+1);
buffer.insert(prePos, tmp.toString());
}
}
System.out.println(buffer.toString());
}
}
你说的是汉字(HZ)编码格式(一种使用ASCII码来表示汉字的方法,多在海外华人中使用)
,不是UTF-8的编码格式。
#7
mark
#8
ttt = new String(ttt.getBytes(), "UTF-8"); //这时ttt是utf-8编码大家应该没有异议吧?
ttt怎么会是utf-8呢?? ttt初始化时String ttt = "我是中文"; 根据jvm安装时的默认字符集设置,这条语句就己经表明ttt是gb2312了,而ttt = new String(ttt.getBytes(), "UTF-8");之后,ttt当然还是gb2312,而且所有字符串的输入输出都是gb2312。而这条语句中,ttt.getBytes()获取的是默认字符集gb2312的字节流数组,然后你又强制把这个本是gb2312的字节数组按utf-8生成字gb2312字符串,这样不错才怪。硬要把一只猫还原成一只狗,明显不尊重客观规律。
如果文件本来就是utf-8编码,可以将其读到字节数组中,如b[],然后再产生gb2312字符串:String s=new String(b,"utf-8");请注意在这里utf-8的作用:Constructs a new String by decoding the specified array of bytes using the specified charset
不对之处,多多指教。
ttt怎么会是utf-8呢?? ttt初始化时String ttt = "我是中文"; 根据jvm安装时的默认字符集设置,这条语句就己经表明ttt是gb2312了,而ttt = new String(ttt.getBytes(), "UTF-8");之后,ttt当然还是gb2312,而且所有字符串的输入输出都是gb2312。而这条语句中,ttt.getBytes()获取的是默认字符集gb2312的字节流数组,然后你又强制把这个本是gb2312的字节数组按utf-8生成字gb2312字符串,这样不错才怪。硬要把一只猫还原成一只狗,明显不尊重客观规律。
如果文件本来就是utf-8编码,可以将其读到字节数组中,如b[],然后再产生gb2312字符串:String s=new String(b,"utf-8");请注意在这里utf-8的作用:Constructs a new String by decoding the specified array of bytes using the specified charset
不对之处,多多指教。
#9
goingon99() ,按照你说的方法,String s=new String(b,"utf-8");我已经解决了问题,但是还有一个bug,就是比如说我读取一个aspx页面的内容(.net页面默认的编码是utf-8),然后输出,但是发现有一些中文会成为半角的?,这是为什么?
比如说,我的test.aspx文件内容如下
<%@ Page language="c#" Codebehind="test.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.test" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>test</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P>
<FONT face="宋体">最新汇率</FONT></P>
<P><FONT face="宋体">你是不是UTF-8编码</FONT></P>
</form>
</body>
</HTML>
====================================================================
我的程序读取的字节流如下
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Wed, 22 Sep 2004 08:55:00 GMT
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Set-Cookie: ASP.NET_SessionId=pph0zr55unl5xi55vwesam55; path=/
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 709
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>test</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form name="Form1" method="post" action="test.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDwtMTI3OTMzNDM4NDs7PmMJphZhg8Y4W2bdz1z22yBMBwsN" />
<P>
<FONT face="宋体">�?新汇�?</FONT></P>
<P><FONT face="宋体">你是不是UTF-8编码</FONT></P>
</form>
</body>
</HTML>
==================================================================
以上最新汇率几个字变成了“?新汇?”
这又是为什么?
比如说,我的test.aspx文件内容如下
<%@ Page language="c#" Codebehind="test.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.test" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>test</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P>
<FONT face="宋体">最新汇率</FONT></P>
<P><FONT face="宋体">你是不是UTF-8编码</FONT></P>
</form>
</body>
</HTML>
====================================================================
我的程序读取的字节流如下
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Wed, 22 Sep 2004 08:55:00 GMT
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Set-Cookie: ASP.NET_SessionId=pph0zr55unl5xi55vwesam55; path=/
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 709
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>test</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form name="Form1" method="post" action="test.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDwtMTI3OTMzNDM4NDs7PmMJphZhg8Y4W2bdz1z22yBMBwsN" />
<P>
<FONT face="宋体">�?新汇�?</FONT></P>
<P><FONT face="宋体">你是不是UTF-8编码</FONT></P>
</form>
</body>
</HTML>
==================================================================
以上最新汇率几个字变成了“?新汇?”
这又是为什么?
#10
再顶一次,没人回答也结帖了。
#11
能否把你的程序贴出来
#1
String ttt = "我是中文";这行中的字符是Unicode编码
#2
那现在这么问吧,我从文件中获得了一些字符串,这些字符串是采用的utf-8编码,我现在想把他转化成人们可读的gb2312怎么做?谢谢了!
#3
假设String str;//为utf-8
new String(str.getBytes(),"utf-8");这样解码应该久可以了
new String(str.getBytes(),"utf-8");这样解码应该久可以了
#4
假设String str;//为utf-8
new String(str.getBytes("GBK),"utf-8");
new String(str.getBytes("GBK),"utf-8");
#5
为了让大家可以测试,也避免浪费大家宝贵的时间,你们可以做一下测试
test.java
public class test {
public static void main(String args[]) {
String ttt = "我是中文";
try {
ttt = new String(ttt.getBytes(), "UTF-8"); //这时ttt是utf-8编码大家应该没有异议吧?
System.out.println("ttt = " + ttt);
ttt = new String(ttt.getBytes("GBK"), "UTF-8"); //这里怎么写才能让ttt变为“我是中文”
System.out.println("ttt = " + ttt);
}
catch (Exception e) {}
}
}
test.java
public class test {
public static void main(String args[]) {
String ttt = "我是中文";
try {
ttt = new String(ttt.getBytes(), "UTF-8"); //这时ttt是utf-8编码大家应该没有异议吧?
System.out.println("ttt = " + ttt);
ttt = new String(ttt.getBytes("GBK"), "UTF-8"); //这里怎么写才能让ttt变为“我是中文”
System.out.println("ttt = " + ttt);
}
catch (Exception e) {}
}
}
#6
package com.boasoft;
class Swap
{
public static void main(String args[])
{
StringBuffer buffer = new StringBuffer("這邊這個東東");
StringBuffer tmp = new StringBuffer();
int prePos = 0;
int pos = 0;
while ( (pos = buffer.indexOf("&#")) >= 0 )
{
prePos = pos;
pos = buffer.indexOf(";", prePos);
if ( pos >= 0 && pos > (prePos+2) )
{
tmp.setLength(0);
tmp.append ( (char)Integer.parseInt(buffer.substring(prePos+2, pos)) );
buffer.delete (prePos, pos+1);
buffer.insert(prePos, tmp.toString());
}
}
System.out.println(buffer.toString());
}
}
你说的是汉字(HZ)编码格式(一种使用ASCII码来表示汉字的方法,多在海外华人中使用)
,不是UTF-8的编码格式。
class Swap
{
public static void main(String args[])
{
StringBuffer buffer = new StringBuffer("這邊這個東東");
StringBuffer tmp = new StringBuffer();
int prePos = 0;
int pos = 0;
while ( (pos = buffer.indexOf("&#")) >= 0 )
{
prePos = pos;
pos = buffer.indexOf(";", prePos);
if ( pos >= 0 && pos > (prePos+2) )
{
tmp.setLength(0);
tmp.append ( (char)Integer.parseInt(buffer.substring(prePos+2, pos)) );
buffer.delete (prePos, pos+1);
buffer.insert(prePos, tmp.toString());
}
}
System.out.println(buffer.toString());
}
}
你说的是汉字(HZ)编码格式(一种使用ASCII码来表示汉字的方法,多在海外华人中使用)
,不是UTF-8的编码格式。
#7
mark
#8
ttt = new String(ttt.getBytes(), "UTF-8"); //这时ttt是utf-8编码大家应该没有异议吧?
ttt怎么会是utf-8呢?? ttt初始化时String ttt = "我是中文"; 根据jvm安装时的默认字符集设置,这条语句就己经表明ttt是gb2312了,而ttt = new String(ttt.getBytes(), "UTF-8");之后,ttt当然还是gb2312,而且所有字符串的输入输出都是gb2312。而这条语句中,ttt.getBytes()获取的是默认字符集gb2312的字节流数组,然后你又强制把这个本是gb2312的字节数组按utf-8生成字gb2312字符串,这样不错才怪。硬要把一只猫还原成一只狗,明显不尊重客观规律。
如果文件本来就是utf-8编码,可以将其读到字节数组中,如b[],然后再产生gb2312字符串:String s=new String(b,"utf-8");请注意在这里utf-8的作用:Constructs a new String by decoding the specified array of bytes using the specified charset
不对之处,多多指教。
ttt怎么会是utf-8呢?? ttt初始化时String ttt = "我是中文"; 根据jvm安装时的默认字符集设置,这条语句就己经表明ttt是gb2312了,而ttt = new String(ttt.getBytes(), "UTF-8");之后,ttt当然还是gb2312,而且所有字符串的输入输出都是gb2312。而这条语句中,ttt.getBytes()获取的是默认字符集gb2312的字节流数组,然后你又强制把这个本是gb2312的字节数组按utf-8生成字gb2312字符串,这样不错才怪。硬要把一只猫还原成一只狗,明显不尊重客观规律。
如果文件本来就是utf-8编码,可以将其读到字节数组中,如b[],然后再产生gb2312字符串:String s=new String(b,"utf-8");请注意在这里utf-8的作用:Constructs a new String by decoding the specified array of bytes using the specified charset
不对之处,多多指教。
#9
goingon99() ,按照你说的方法,String s=new String(b,"utf-8");我已经解决了问题,但是还有一个bug,就是比如说我读取一个aspx页面的内容(.net页面默认的编码是utf-8),然后输出,但是发现有一些中文会成为半角的?,这是为什么?
比如说,我的test.aspx文件内容如下
<%@ Page language="c#" Codebehind="test.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.test" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>test</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P>
<FONT face="宋体">最新汇率</FONT></P>
<P><FONT face="宋体">你是不是UTF-8编码</FONT></P>
</form>
</body>
</HTML>
====================================================================
我的程序读取的字节流如下
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Wed, 22 Sep 2004 08:55:00 GMT
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Set-Cookie: ASP.NET_SessionId=pph0zr55unl5xi55vwesam55; path=/
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 709
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>test</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form name="Form1" method="post" action="test.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDwtMTI3OTMzNDM4NDs7PmMJphZhg8Y4W2bdz1z22yBMBwsN" />
<P>
<FONT face="宋体">�?新汇�?</FONT></P>
<P><FONT face="宋体">你是不是UTF-8编码</FONT></P>
</form>
</body>
</HTML>
==================================================================
以上最新汇率几个字变成了“?新汇?”
这又是为什么?
比如说,我的test.aspx文件内容如下
<%@ Page language="c#" Codebehind="test.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.test" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>test</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P>
<FONT face="宋体">最新汇率</FONT></P>
<P><FONT face="宋体">你是不是UTF-8编码</FONT></P>
</form>
</body>
</HTML>
====================================================================
我的程序读取的字节流如下
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Wed, 22 Sep 2004 08:55:00 GMT
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Set-Cookie: ASP.NET_SessionId=pph0zr55unl5xi55vwesam55; path=/
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 709
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>test</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form name="Form1" method="post" action="test.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDwtMTI3OTMzNDM4NDs7PmMJphZhg8Y4W2bdz1z22yBMBwsN" />
<P>
<FONT face="宋体">�?新汇�?</FONT></P>
<P><FONT face="宋体">你是不是UTF-8编码</FONT></P>
</form>
</body>
</HTML>
==================================================================
以上最新汇率几个字变成了“?新汇?”
这又是为什么?
#10
再顶一次,没人回答也结帖了。
#11
能否把你的程序贴出来