zoukankan      html  css  js  c++  java
  • java GBK字符转换成为UTF-8编码字符

    import java.util.HashMap;
    import java.util.Map;
    /**
     * 创建日期: 2014-04-18 10:36:25
     * 作者: 黄飞
     * mail:huangfei8087@163.com 834865081@qq.com
     * ©版权归作者全部
     * */
    public class ConverFromGBKToUTF8 {
    	public static void main(String[] args) {
    		try {
    			ConverFromGBKToUTF8 convert = new ConverFromGBKToUTF8();
    			String fullStr = convert.gbk2utf8("半角变拳脚,quanjiao "); 
    			
    			System.out.println( fullStr  ) ;
    		} catch (Exception e) {
    			e.printStackTrace(); 
    		}
    	}
    	
    	private StringUtil chinese(String chinese){
    		StringUtil util = new StringUtil();
    		char[]chars = chinese.toCharArray() ;
    		int index = 0 ;
    		StringBuffer buffer = new StringBuffer();
    		for(char c : chars){
    			String temp = String.valueOf(c) ;
    			if(temp.getBytes().length == 1){
    				util.map.put( index , temp ) ;
    			}else{
    				buffer.append( temp );
    			}
    			index++;
    		}
    		util.chinese = buffer.toString() ;
    		return util ;
    	}
    	
    	public String gbk2utf8(String chenese) {
    		StringUtil strUtil = chinese( chenese ) ;
    		char c[] = strUtil.chinese.toCharArray( ) ;
    		byte[] fullByte = new byte[3 * c.length];
    		for (int i = 0; i < c.length; i++) {
    			int m = (int) c[i];
    			String word = Integer.toBinaryString(m);
    
    			StringBuffer sb = new StringBuffer();
    			int len = 16 - word.length();
    			for (int j = 0; j < len; j++) {
    				sb.append("0");
    			}
    			sb.append(word);
    			sb.insert(0, "1110");
    			sb.insert(8, "10");
    			sb.insert(16, "10");
    
    			String s1 = sb.substring(0, 8);
    			String s2 = sb.substring(8, 16);
    			String s3 = sb.substring(16);
    
    			byte b0 = Integer.valueOf(s1, 2).byteValue();
    			byte b1 = Integer.valueOf(s2, 2).byteValue();
    			byte b2 = Integer.valueOf(s3, 2).byteValue();
    			byte[] bf = new byte[3];
    			bf[0] = b0;
    			fullByte[i * 3] = bf[0];
    			bf[1] = b1;
    			fullByte[i * 3 + 1] = bf[1];
    			bf[2] = b2;
    			fullByte[i * 3 + 2] = bf[2];
    		}
    		String reutrnStr = null ;
    		try {
    			reutrnStr = new String(fullByte, "UTF-8");
    		} catch (Exception e) {
    		} 
    		StringBuffer returnBuffer = new StringBuffer(  reutrnStr );
    		for(Map.Entry<Integer, String> entry : strUtil.map.entrySet()){
    			returnBuffer.insert(entry.getKey() , entry.getValue() ) ;
    		}
    		
    		return returnBuffer.toString() ;
    	}
    }
    
    class StringUtil{
    	public String chinese ; 
    
    	public Map<Integer , String> map = new HashMap<Integer, String>();
    }

  • 相关阅读:
    Avizo
    NEWS
    HOWTO
    InstallShield 2012 Spring优惠升级到最新版本(2015.4.30之前)
    Windows系统补丁KB2962872导致InstallShield无法启动(解决方案已更新)
    HOWTO: InstallScript MSI工程取Log
    安装软件列表
    阿里云推荐码 hut29f
    ios 缺少合规证明
    ios开发错误之: Undefined symbols for architecture x86_64
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5328619.html
Copyright © 2011-2022 走看看