java char打印乱码_ByteBuffer.asCharBuffer打印字符串乱码

背景

下面代码会直接输出乱码。原因是:getBytes需要指定字符编码

ByteBuffer byteBuffer = ByteBuffer.wrap("开源中国".getBytes());

CharBuffer buffer = byteBuffer.asCharBuffer();

System.out.println(buffer.toString());

修改后就能符合预期:

ByteBuffer byteBuffer = ByteBuffer.wrap("开源中国".getBytes(StandardCharsets.UTF_16));

CharBuffer buffer = byteBuffer.asCharBuffer();

System.out.println(buffer.toString());

原因

字符编码和编码的方式不一致导致的。

getBytes 其实就是把字符串编码按照默认的UTF-8进行编码称为字节数组(看源码可知).

byteBuffer.asCharBuffer()也就是转为UTF-8的CharBuffer。JAVA内部使用的是UTF-16

下面会证明:

ByteBuffer byteBuffer = ByteBuffer.wrap("开源中国".getBytes());

System.out.println(byteBuffer.limit()); // 12 每个字符3字节

// 把 ByteBuffer 使用UTF-8解码为UTF-16的CharBuffer然后toString输出

System.out.println(StandardCharsets.UTF_8.decode(byteBuffer).toString()); // 结果能正常输出

byteBuffer.rewind();

CharBuffer buffer = byteBuffer.asCharBuffer();

System.out.println(buffer.toString());

小结

JAVA要输出CharBuffer或者char数组,会直接认为是UTF-16编码来获取对应的代码点,最终找到映射的字符

String.getBytes:使用默认编码UTF-8(Windows会不一样)进行编码为对应字节

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值