字符串-不同的编码格式下所占用的字节数【转】
2015-06-06 17:37
721 查看
英文字母和中文汉字在不同的编码格式下,所占用的字节数也是不同的,我们可以通过下面的例子来看看在一些常见的编码格式下,一个英文字母和一个中文汉字分别占用多少字节。
import java.io.UnsupportedEncodingException;
public class EncodeTest {
public static void printByteLength(String s, String encodingName) {
System.out.print("字节数:");
try {
System.out.print(s.getBytes(encodingName).length); }
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(";编码:" + encodingName); }
public static void main(String[] args) {
String en = "A"; String ch = "人"; // 计算一个英文字母在各种编码下的字节数
System.out.println("英文字母:" + en);
EncodeTest.printByteLength(en, "GB2312");
EncodeTest.printByteLength(en, "GBK");
EncodeTest.printByteLength(en, "GB18030");
EncodeTest.printByteLength(en, "ISO-8859-1");
EncodeTest.printByteLength(en, "UTF-8");
EncodeTest.printByteLength(en, "UTF-16");
EncodeTest.printByteLength(en, "UTF-16BE");
EncodeTest.printByteLength(en, "UTF-16LE");
System.out.println();
// 计算一个中文汉字在各种编码下的字节数
System.out.println("中文汉字:" + ch);
EncodeTest.printByteLength(ch, "GB2312");
EncodeTest.printByteLength(ch, "GBK");
EncodeTest.printByteLength(ch, "GB18030");
EncodeTest.printByteLength(ch, "ISO-8859-1");
EncodeTest.printByteLength(ch, "UTF-8");
EncodeTest.printByteLength(ch, "UTF-16");
EncodeTest.printByteLength(ch, "UTF-16BE");
EncodeTest.printByteLength(ch, "UTF-16LE"); } }
结果:
英文字母:A 字节数:1;编码:GB2312 字节数:1;编码:GBK 字节数:1;编码:GB18030 字节数:1;编码:ISO-8859-1 字节数:1;编码:UTF-8 字节数:4;编码:UTF-16 字节数:2;编码:UTF-16BE 字节数:2;编码:UTF-16LE
中文汉字:人 字节数:2;编码:GB2312 字节数:2;编码:GBK 字节数:2;编码:GB18030 字节数:1;编码:ISO-8859-1 字节数:3;编码:UTF-8 字节数:4;编码:UTF-16 字节数:2;编码:UTF-16BE 字节数:2;编码:UTF-16LE
import java.io.UnsupportedEncodingException;
public class EncodeTest {
public static void printByteLength(String s, String encodingName) {
System.out.print("字节数:");
try {
System.out.print(s.getBytes(encodingName).length); }
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(";编码:" + encodingName); }
public static void main(String[] args) {
String en = "A"; String ch = "人"; // 计算一个英文字母在各种编码下的字节数
System.out.println("英文字母:" + en);
EncodeTest.printByteLength(en, "GB2312");
EncodeTest.printByteLength(en, "GBK");
EncodeTest.printByteLength(en, "GB18030");
EncodeTest.printByteLength(en, "ISO-8859-1");
EncodeTest.printByteLength(en, "UTF-8");
EncodeTest.printByteLength(en, "UTF-16");
EncodeTest.printByteLength(en, "UTF-16BE");
EncodeTest.printByteLength(en, "UTF-16LE");
System.out.println();
// 计算一个中文汉字在各种编码下的字节数
System.out.println("中文汉字:" + ch);
EncodeTest.printByteLength(ch, "GB2312");
EncodeTest.printByteLength(ch, "GBK");
EncodeTest.printByteLength(ch, "GB18030");
EncodeTest.printByteLength(ch, "ISO-8859-1");
EncodeTest.printByteLength(ch, "UTF-8");
EncodeTest.printByteLength(ch, "UTF-16");
EncodeTest.printByteLength(ch, "UTF-16BE");
EncodeTest.printByteLength(ch, "UTF-16LE"); } }
结果:
英文字母:A 字节数:1;编码:GB2312 字节数:1;编码:GBK 字节数:1;编码:GB18030 字节数:1;编码:ISO-8859-1 字节数:1;编码:UTF-8 字节数:4;编码:UTF-16 字节数:2;编码:UTF-16BE 字节数:2;编码:UTF-16LE
中文汉字:人 字节数:2;编码:GB2312 字节数:2;编码:GBK 字节数:2;编码:GB18030 字节数:1;编码:ISO-8859-1 字节数:3;编码:UTF-8 字节数:4;编码:UTF-16 字节数:2;编码:UTF-16BE 字节数:2;编码:UTF-16LE
相关文章推荐
- 企业级应用WebLogic11g集群配置与项目实施_WebLogic部署培训教程
- HDFS数据完整性
- POSIX 线程(一)
- 大端小端的证明
- 第14周-输入输出流,文本文件-项目1 - 小玩文件(2)
- 字典的相关操作
- leetcode - Count Complete Tree Nodes
- C#高级编程第一天
- layoutSubviews总结
- 程序员面试宝典 7 - 指针与引用
- Unity3D优化总结(一)
- leetcode 208 【trie】
- linux内核链表使用
- 单例模式Singleton – 学习HeadFirst设计模式记录
- linux系统常用命令
- LeetCode | Median of Two Sorted Arrays
- 解决chrome32以上版本,非商店扩展程序被停用的问题
- 中国剩余定理
- VMware虚拟机安装+Linux操作系统安装视频教程_Linux实验环境安装
- 后台执行screen使用心得