博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
几种不同的字符编码方式
阅读量:4552 次
发布时间:2019-06-08

本文共 738 字,大约阅读时间需要 2 分钟。

1.gbk

gbk编码格式中一个中文占两个字节,英文占一个字节

String s = "你好ABC";

byte[] bytes = s.getBytes("gbk");
//gbk编码中文占两个字节,英文占一个字节
for (byte b : bytes) {
//把字节转化成int以16进制显示(只填充了int的低八位),与0xff(1111 1111)相与只取低八位。
System.out.print(Integer.toHexString(b & 0xff)+" ");
}

执行结果:

c4 e3 ba c3 41 42 43 

2.utf-8

byte[] bytes1 = s.getBytes("utf-8");

//utf-8中文占三个字节,英文占1个字节
for (byte b : bytes1) {
System.out.print(Integer.toHexString(b & 0xff)+" ");
}
System.out.println();

执行结果:

e4 bd a0 e5 a5 bd 41 42 43

3.utf-16be

//java是双字节编码utf-16be(编译形成class文件后),中文占两个字节,英文占两个字节

byte[] bytes2 = s.getBytes("utf-16be");
for (byte b : bytes2) {
System.out.print(Integer.toHexString(b & 0xff)+" ");
}

执行结果:

4f 60 59 7d 0 41 0 42 0 43 

 

转载于:https://www.cnblogs.com/simple96/p/7222040.html

你可能感兴趣的文章
thymeleaf:字符串拼接+输出单引号
查看>>
springboot:集成fastjson(教训)
查看>>
网络流 Edmons-Karp 算法讲解
查看>>
「NOIP2018模拟9.10」公约数 - 找规律 - gcd
查看>>
使用java理解程序逻辑(15)
查看>>
bzoj 1879 状压dp
查看>>
python 一些特殊用法和坑
查看>>
WIFI密码破解全攻略
查看>>
c++string各种函数
查看>>
errno.h含义
查看>>
字典树(模型体)
查看>>
盒模型详解
查看>>
bzoj2157 旅游
查看>>
bzoj5016 [Snoi2017]一个简单的询问
查看>>
poj2417 bzoj3239 Discrete Logging(bsgs)
查看>>
UVa10054 - The Necklace(欧拉回路【输出带来的麻烦)
查看>>
string和stringbuffer的区别 集合的作用 ArrayList vector linklist hashmap hashtable collection和collections...
查看>>
6月27日 ajax
查看>>
iOS开发之画图板(贝塞尔曲线)
查看>>
4嵌入式作业io
查看>>