import java.io.UnsupportedEncodingException;
public class main {
public static void main(String[] args) throws UnsupportedEncodingException{
byte [] b= new byte[]{(byte) 0xcc,(byte) 0xe1,(byte) 0xbd,(byte) 0xbb};
String s = new String(b,"ISO8859-1");
System.out.println(s);
print(s.getBytes("ISO8859-1"));
print(s.getBytes("GBK"));
print(s.getBytes("UTF-16"));
System.out.println(new String(s.getBytes("ISO8859-1"),"gbk"));
String ss = "中文";
print(ss.getBytes("UTF-16"));
print(ss.getBytes("ISO8859-1"));
}
static void print(byte [] b){
for(byte _b : b){
String s = Integer.toHexString(_b&0xff);
if(s.length()==1){
s = "0"+s;
}
System.out.print(s + " ");
}
System.out.println();
}
}
浏览器发送GBK字节到中间件,中间把这些字节都当作ISO8859-1字符处理,直接new String()打印出来的肯定是乱码,因为(byte) 0xcc,(byte) 0xe1,(byte) 0xbd,(byte) 0xbb对应的ISO8859-1字符是找不到的!
我们需要从字符串中重新把GBK字节拿出来,构造出原来的中文字符串。原来如代码所示。
分享到:
相关推荐
String str3 = new String(buf3,"ISO8859-1");//错误解码 //编码解码4:错误编码正确解码 String str4 = "你好"; byte[] buf4 = str4.getBytes("ISO8859-1");//错误编码 String str4 = new String(buf4,"GBK")...
解决Struts2上传时候使用UTF-8的冲突问题... * 对于请求流,使用的ISO-8859-1编码方式进行,如果发现请求内容中出现名称乱码,请使用new String(str.getBytes("ISO-8859-1"),"GBK")进行编码转换。 包里面已经含有源代码
! public static String change(String str) { ... temp = new String(str.getBytes("ISO-8859-1"), "GBK").trim(); } catch (Exception e) { e.printStackTrace(); } return temp; }%>
有关java面试的一些问题Public ... try { tempStr = new String(str.getBytes("ISO-8859-1"), "GBK"); tempStr = tempStr.trim(); } catch (Exception e) { System.err.println(e.getMessage()); } return tempStr; }
String result = new String(str.getBytes("iso-8859-1"), "gbk"); return result; }%> String msg = "调查报告已经提交成功。"; try { String txrq = getStringGBK(request, "txrq"); // 填写日期 ...
String result = new String(str.getBytes("iso-8859-1"), "gbk"); return result; }%> WritableWorkbook book = null; String path = ""; String dateString = ""; String msg = "调查报告已经提交成功。";...
/** *base64加密与解密 ...* 2、解密:String keywords =new String(Base64.decode(keywords),"unicode"); *本JS使用方法如下: * 1、调用encode64(str)方法加密字符 * 2、调用decode64(str)方法解密字符 **/
+ String.valueOf(symbol3) + String.valueOf(symbol1); strReturn = myport.sendAT(atCommand); if (strReturn.indexOf("OK") != -1 && strReturn.indexOf("+CMGS") != -1) { System.out.println("短信发送...
* charsetName:字符集名 例如 : "GBK"、"UTF-8" 、"ISO-8859-1" * * 不常用 * 3.String(byte[] bytes) 根据默认字符集将字节数组转换为字符串 * 4.String(byte[] bytes, String ...
27、String s = new String("xyz");创建了几个String Object? 两个 28、设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。 以下程序使用内部类实现线程,对j增减的时候没有考虑顺序...
30、String s = new String("xyz");创建了几个String Object? 两个 31、EJB包括(SessionBean,EntityBean)说出他们的生命周期,及如何管理事务的? SessionBean: Stateless Session Bean 的生命周期是由容器...