Java字符串编码 [英] Java String encoding

查看:104
本文介绍了Java字符串编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 hello world.getBytes(UTF-8)之间有什么区别? 

  Charset.forName(UTF-8)。encode(hello world)。array(); 


第二个代码在大多数情况下产生一个0字节的字节数组。

解决方案

您的第二个代码段使用 ByteBuffer。 array() ,它只返回支持 ByteBuffer 的数组。这可能比 ByteBuffer 的内容更长。



基本上,如果您想从 String 中的字节[] 使用第一种方法:)您可以使用其他方式来处理 ByteBuffer 将其转换为 byte [] ,但由于 String.getBytes(Charset)可用和方便,我只是使用...



示例代码从 ByteBuffer 中检索字节:

  ByteBuffer buffer = Charset.forName (UTF-8)。encode(hello world); 
byte [] array = new byte [buffer.limit()];
buffer.get(array);
System.out.println(array.length); // 11
System.out.println(array [0]); // 104(编码'h')


What´s the difference between

"hello world".getBytes("UTF-8");

and

 Charset.forName("UTF-8").encode("hello world").array();

? The second code produces a byte array with 0-bytes at the end in most cases.

解决方案

Your second snippet uses ByteBuffer.array(), which just returns the array backing the ByteBuffer. That may well be longer than the content written to the ByteBuffer.

Basically, I would use the first approach if you want a byte[] from a String :) You could use other ways of dealing with the ByteBuffer to convert it to a byte[], but given that String.getBytes(Charset) is available and convenient, I'd just use that...

Sample code to retrieve the bytes from a ByteBuffer:

ByteBuffer buffer = Charset.forName("UTF-8").encode("hello world");
byte[] array = new byte[buffer.limit()];
buffer.get(array);
System.out.println(array.length); // 11
System.out.println(array[0]);     // 104 (encoded 'h')

这篇关于Java字符串编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆