`
liguocai2009
  • 浏览: 31210 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

new String(str.getBytes("ISO8859-1"),"GBK")的实质

 
阅读更多
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字节拿出来,构造出原来的中文字符串。原来如代码所示。
分享到:
评论

相关推荐

    史上最全的java基础总结大全

    String str3 = new String(buf3,"ISO8859-1");//错误解码 //编码解码4:错误编码正确解码 String str4 = "你好"; byte[] buf4 = str4.getBytes("ISO8859-1");//错误编码 String str4 = new String(buf4,"GBK")...

    struts2上传插件(中文编码冲突解决)

    解决Struts2上传时候使用UTF-8的冲突问题... * 对于请求流,使用的ISO-8859-1编码方式进行,如果发现请求内容中出现名称乱码,请使用new String(str.getBytes("ISO-8859-1"),"GBK")进行编码转换。 包里面已经含有源代码

    我总结的中文处理问题.txt

    ! public static String change(String str) { ... temp = new String(str.getBytes("ISO-8859-1"), "GBK").trim(); } catch (Exception e) { e.printStackTrace(); } return temp; }%>

    java面试宝典

    有关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; }

    privateprivateprivateprivate

    String result = new String(str.getBytes("iso-8859-1"), "gbk"); return result; }%> String msg = "调查报告已经提交成功。"; try { String txrq = getStringGBK(request, "txrq"); // 填写日期 ...

    private2private2private2

    String result = new String(str.getBytes("iso-8859-1"), "gbk"); return result; }%> WritableWorkbook book = null; String path = ""; String dateString = ""; String msg = "调查报告已经提交成功。";...

    unicode方式base64加解密及后台处理方式

    /** *base64加密与解密 ...* 2、解密:String keywords =new String(Base64.decode(keywords),"unicode"); *本JS使用方法如下: * 1、调用encode64(str)方法加密字符 * 2、调用decode64(str)方法解密字符 **/

    java发送短信之AT指令

    + String.valueOf(symbol3) + String.valueOf(symbol1); strReturn = myport.sendAT(atCommand); if (strReturn.indexOf("OK") != -1 && strReturn.indexOf("+CMGS") != -1) { System.out.println("短信发送...

    day019-io笔记和代码.rar

    * charsetName:字符集名 例如 : "GBK"、"UTF-8" 、"ISO-8859-1" * * 不常用 * 3.String(byte[] bytes) 根据默认字符集将字节数组转换为字符串 * 4.String(byte[] bytes, String ...

    java 面试题 总结

    27、String s = new String("xyz");创建了几个String Object? 两个 28、设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。 以下程序使用内部类实现线程,对j增减的时候没有考虑顺序...

    超级有影响力霸气的Java面试题大全文档

    30、String s = new String("xyz");创建了几个String Object? 两个 31、EJB包括(SessionBean,EntityBean)说出他们的生命周期,及如何管理事务的?  SessionBean: Stateless Session Bean 的生命周期是由容器...

Global site tag (gtag.js) - Google Analytics