字符编码问题
2016-03-04 15:59
375 查看
ASCII 美国标准编码表
ISO8859-1 :拉丁编码表
GB2312 :中文编码
GBK和GB18030:兼容GB2312
Unicode:国际标准编码表
UFT-8:支持中文编码
运行结果:
ISO8859-1 :拉丁编码表
GB2312 :中文编码
GBK和GB18030:兼容GB2312
Unicode:国际标准编码表
UFT-8:支持中文编码
package com.example.io; import java.io.UnsupportedEncodingException; import java.util.Arrays; public class Example32 { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub String str="北京"; byte[] b1=str.getBytes(); //使用默认的编码表 byte[] b2=str.getBytes("GBK"); //使用GBK编码表 System.out.println(Arrays.toString(b1)); // 打印出字节数组的字符串形式 System.out.println(Arrays.toString(b2)); //系统默认就是GBK byte[] b3 = str.getBytes("UTF-8"); // 使用UTF-8编码 //gbk的编码gbk编码输出(系统默认) String result1 = new String(b1, "GBK"); System.out.println(result1); //gbk的编码gbk编码输出 String result2 = new String(b2, "GBK"); System.out.println(result2); //utf-8编码UFT-8输出 String result3 = new String(b3, "UTF-8"); System.out.println(result3); //gbk的编码ISO8859-1编码输出 String result4 = new String(b2, "ISO8859-1"); System.out.println(result4); } }
运行结果:
import java.util.*; public class Example33 { public static void main(String[] args) throws Exception { String str = "北京"; byte[] b = str.getBytes("GBK"); String temp = new String(b, "ISO8859-1"); System.out.println(temp); // 用错误的码表解码,打印出了乱码 byte[] b1 = temp.getBytes("ISO8859-1"); // 再使用错误的码表编码 String result = new String(b1, "GBK"); // 用正确的码表解码 System.out.println(result); // 打印出正确的结果 } }
相关文章推荐
- NPOI 导出Excel图片 (网络)
- 微信:实现 pc 端 扫描二维码登陆 的误区
- 长安.ARM体系结构分析
- 水晶报表中使用条码字体
- java 二叉查找树(搜索树、排序树)实现
- jquery validation plugin
- Objective-c 中的变量
- Homework1——印象最深刻的error
- 经典语句
- LINQ高级编程 笔记
- /usr/local/php/bin/phpize生成configure文件时报错 Cannot find autoconf. Please check your autoconf
- 记一次基于Unity的Profiler性能分析
- 浅谈AudioManager
- cxf返回Map
- iOS如何优雅的使用sqlite数据库
- 分布式文件系统选型
- 初学者如何在博客园发布博客
- 多行文本未知高度垂直居中-
- CSS选择器
- 初试Android基于Vuforia开发AR