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>();
}
java GBK字符转换成为UTF-8编码字符的更多相关文章
- java 字节→字符转换流
OutputStreamWriter:把字节输出流对象转成字符输出流对象 InputStreamReader:把字节输入流对象转成字符输入流对象 FileWriter和FileReader分别是Out ...
- JAVA IO分析一:File类、字节流、字符流、字节字符转换流
因为工作事宜,又有一段时间没有写博客了,趁着今天不是很忙开始IO之路:IO往往是我们忽略但是却又非常重要的部分,在这个讲究人机交互体验的年代,IO问题渐渐成了核心问题. 一.File类 在讲解File ...
- is 和 == 的区别,utf和gbk的转换,join用法
is 和 == 的区别 # is 比较的是内存地址 # == 比较的是值 a = 'alex' b = 'alex' #int,str(小数据池)会被缓存,为了节约内存 print(id(a),id( ...
- Java第三十三天,IO操作(续集),字符转换流
计算机都是以二进制码格式存储文件的,但是在读写文件的过程中,每个应用程序都有自己的编码格式.FileWrite和FileRead类是通过查询系统默认码表进行读写的,因此在自己的系统上能够实现编码的智能 ...
- Java Io 字符流
Java Io 字符流包含: 1. InputStreamReader 它是由byte流解析为char流,并且按照给定的编码解析. 2. OutputStreamWrite 它是char流到byt ...
- 解决GBK字符转UTF-8乱码问题
通过以下方法将GBK字符转成UTF-8编码格式的byte[]数组 package test; import java.io.UnsupportedEncodingException; public c ...
- 完整的java字符串编码转换代码
package book.String; import java.io.UnsupportedEncodingException; /** *//** * 转换字符串的编码 * @author joe ...
- JAVA字符串编码转换常用类
无论是对程序的本地化还是国际化,都会涉及到字符编码的转换的问题.尤其在web应用中常常需要处理中文字符,这时就需要进行字符串的编码转换,将字符串编码转换为GBK或者GB2312.一.关键技术点: ...
- idea Error:(1, 10) java: 需要class, interface或enum, 未结束的字符串文字,Error:(55, 136) java: 非法字符: \65533
1.未结束的字符串文字,Error:(55, 136) java: 非法字符: \65533 这些乱七吧八遭的错误如果很多的话 , 尝试 重新修改下生成目录 修改下语言等级 上述方法都不行 ,还报错 ...
随机推荐
- Wpf 数据绑定实例2
1.简单DataContext绑定 //绑定对象 label.DataContext = Process.GetCurrentProcess(); Xaml代码: <Grid> <G ...
- 带A圈的秘密
真嗒安全策略的罗罗,,害的我和其他的不一样.
- eclipse/myeclipse 变量名自动补全问题
原理是:在输入变量名后,去掉按下空格或=后,代码上屏 以前只知道alt+/调出assist,后来发现可以所有字母都激活content assist(8.1里有写).用起来果然很爽,但是eclipse ...
- 安卓 报错 Check the Eclipse log for stack trace.
移除Android Dependencies就行了. 在properties的Libraries里
- Spring+SpringMVC+Mybatis+MAVEN+Eclipse+项目完整环境搭建
1.新建一个Maven项目,创建父项目. 2.创建子项目模块 3.创建javaWeb项目 4.创建后的项目目录结构 5.Maven文件配置 parent父项目pom.xml文件配置 <?xml ...
- (转)ligerUI 使用教程之Tip介绍与使用
概述: ligertip是ligerUI系列插件中的tooltip类插件,作用是弹一个浮动层,起提示作用 阅读本文要求具备jQuery的基本知识,不然文中的javascript代码不易理解 截 ...
- python3和Python2的区别(被坑太久了)
print函数:(Python3中print为一个函数,必须用括号括起来:Python2中print为class) Python 2 的 print 声明已经被 print() 函数取代了,这意味着我 ...
- 怎样导入SDWebImage
Two ways : 方法1:copy all the files into your project. --下载https://github.com/rs/SDWebImage. --把SD项目co ...
- 简单又强大的联发科手机PhilZ Touch Recovery安装器,详细教程 - 本文出自高州吧
原文地址:http://bbs.gaozhouba.com/thread-19355-1-1.html * * * * * * * * * * * * * * * * * * * * * * * * ...
- 我的Jekyll博客
我在GitHub Page上托管的Jekyll博客地址:http://lastavenger.github.io/