java GBK字符转换成为UTF-8编码字符
来源:互联网 发布:java 如何建立socket 编辑:程序博客网 时间:2024/05/21 18:37
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>();}
0 0
- java GBK字符转换成为UTF-8编码字符
- Java字符编码UTF-8 GBK
- 字符编码GBK,UTF-8
- JAVA字符编码系列二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换
- JAVA字符编码系列二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换
- JAVA字符编码系列二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换
- 关于JAVA字符编码:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换
- JAVA字符编码系列二:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换
- JAVA字符编码系列二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换
- JAVA字符编码系列二:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换
- JAVA字符编码系列二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换
- JAVA字符编码系列二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换
- JAVA字符编码系列二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换
- 关于JAVA字符编码:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换
- 关于JAVA字符编码:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换
- JAVA字符编码乱码问题:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换
- Android字符编码转换,GBK转UTF-8
- 字符编码Unicode UTF-8 GBK的不同和转换
- SZ斐波拉契数列
- 安卓ADB端口被占用/ADB无法使用解决办法
- 面试准备 学习计划
- Codeforces Round #FF (Div. 2/A)/Codeforces447A_DZY Loves Hash(哈希)
- JavaScript Cookies
- java GBK字符转换成为UTF-8编码字符
- ACdream 1064 完美数
- hdu 1788 Chinese remainder theorem again((数学:简单题)
- iOS开发,装腔的语言国际化
- iOS模拟器如何导入图片不丢失exif和tiff数据
- mac的sqlite管理工具:SQLiteManager
- Java开源内容管理CMS系统J4CMS集成到JTM
- 烟锁重楼,情缘若梦终是空
- 深入理解Java内存模型(七)——总结