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 这些乱七吧八遭的错误如果很多的话 , 尝试 重新修改下生成目录 修改下语言等级 上述方法都不行 ,还报错 ...
随机推荐
- Android-SVN
服务器启动svn服务 svnserve -d -r /home/wbp/svn/actia/ 1 .svn 重新定位location , 改变新仓库的uuid , 今天操作SVN Client 发现 ...
- (转)Android调用系统自带的文件管理器进行文件选择并获得路径
Android区别于iOS的沙盒模式,可以通过文件浏览器浏览本地的存储器.Android API也提供了相应的接口. 基本思路,先通过Android API调用系统自带的文件浏览器选取文件获得URI, ...
- 编译安装php时提示Cannot find MySQL header files的解决方法
php的配置文件中有一行--with-mysql=/usr/local/mysql ,安装的时候提示:configure: error: Cannot find MySQL header files ...
- css水平居中的小小探讨
水平居中是常用的几种布局方式之一.主要分为行内元素的居中,块元素的居中.块元素的居中还分为固定宽度的居中,不定宽度的居中.行内元素的居中,使用text-align:center就可以实现,已知宽度的块 ...
- centos 6.X 安装输入法
1.打开终端 su 输入 密码 yum install "@Chinese Support" 2.接下来是启用中文输入法的操作 系统 ->首选项 ->输入法 3.在弹出 ...
- undefined 和 null 的异同
在javascript中,undefined和Null是两个比较特殊的值.但有时候在判断时就有点迷糊.依个人浅见,整理如下: 1.数据类型 众多周知,在javascript中存在五种基本类型,即und ...
- 字符串转换成JSON(js)
JSON.parse('{"site":"zlog"}'); 使用JSON.parse需严格遵守JSON规范, 属性都需用双引号引起来, 一定是双引号!! 相反 ...
- 11_RHEL安装Maya2015
1. 解压 tar -xvf ./Autodesk_Maya_English_2015_Linux_64bit.tgz 2. 运行 ./setup 2.1补充 如果提示缺少 libpng12.so.0 ...
- 34 Search for a Range(目标数的范围Medium)
题目意思:递增数组,找到目标数的范围,找不到则返回[-1,-1] 思路:折半查找 class Solution { public: vector<int> searchRange(vect ...
- CodeForces 478B 第六周比赛B题
B - B Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Descriptio ...