日期:2014-05-18  浏览次数:20646 次

请问一下这句话,到底是什么意思?
请问:String   str= "ab ";
byte[]   temp=str.getBytes( "iso-8859-1 ");到底怎么解释它的用途呢?
是不是,temp数组是一个以 "iso-8859-1 "字节码的数组?
好多jsp转换中文问题都是用:   String   temp=new   String(   str.getBytes( "iso-8859-1 "), "gb2312 ");
这句话的意思是不是说,返回一个编码为 "gb2312 "的字符串?如果把这句话的 "iso-8859-1 "换成 "gb2312 "就又是乱码了.是不是说只有 "iso-8859-01 "的字节码才能转换成 "gb2312 "的字节码.我真的搞得头都晕了,请各位帮忙分析一下吧.

------解决方案--------------------
用什么编码,关键是看你的字符数组是什么编码的。
如果String s是ISO-8859-1编码的,那么当然要用getBytes( "iso-8859-1 ")来取字节数组才准确。如果按其它编码方式来取,就可能出现错误。

如果一开始生成String的时候,本来是GB2312的字节数组,却被处理器以ISO-8859-1的方法编码成字符串,,那么字符串肯定是乱码。所以只好反过来,按ISO-8859-1将其转换为字节数组,再用正确的GB2312编码成字符串,才是正常的。
------解决方案--------------------
test1.html
<html>
<head>
<meta http-equiv= "content-type " content= "text/html; charset=“GB2312 ">
</head>

<body>
<FORM action= "test2.jsp " method=post>
<INPUT type= "checkbox " name= "fruits " value= "Apple "> 苹果 <br>
<INPUT type= "checkbox " name= "fruits " value= "香蕉 "> 香蕉 <br>
<INPUT type= "checkbox " name= "fruits " value= "橘子 "> 橘子 <br>
<br>
<INPUT type= "submit " value= "确定 ">
</FORM>
</body>
</html>
------解决方案--------------------
并不是所有的页面都需要那样转码的.
request.setPageEncoding( "GB2312 ");//试试行不行
<%for(int i = 0; i < picked.length;i++){
String temp=new String( picked[i].getBytes( "iso-8859-1 "), "gb2312 ");

out.println( " <li> " + temp);
}
%>

------解决方案--------------------
请问:String str= "ab ";
byte[] temp=str.getBytes( "iso-8859-1 ");到底怎么解释它的用途呢?

答:

String str= "ab "等于 String str= new String( "ab ")
java中 把String 当作一个类,
byte[] temp=str.getBytes( "iso-8859-1 "); 是调用String类的一个方法,把当前的str对象以“iso-9951-1”的字符编码方式转换,赋值给temp
------解决方案--------------------
路过看看