如何在Java中将ASCII字符串转换为UTF-8字符串? [英] How do I convert from ASCII String to UTF-8 String in Java?
问题描述
如标题所示,如何在Java中将ASCII字符串转换为UTF-8字符串?
as titled, how do I convert an ASCII String to an UTF-8 String in Java?
谢谢!
我的情况确实是我读了一个中文字符串,当我输出它时,全都是乱码.我认为问题可能出在编码上.那么,如何正确地将字符串从乱码转换为正确的语言设置字符?
My situation is really that I read in a Chinese String, and when I output it, it's all gibberish. I thought the problem might lie in the encoding. So, how do I properly convert the String from the gibberish to the proper language set characters?
推荐答案
Java中没有诸如"ASCII字符串"或"UTF-8字符串"之类的东西.到您有一个String
对象时,它只是一个UTF-16代码单元的序列.没有记录它最初是使用ASCII还是使用UTF-8从字节数组中解码来解释字节的.
There's no such thing as an "ASCII string" or a "UTF-8 string" in Java. By the time you've got a String
object, it's just a sequence of UTF-16 code units. There's no record of whether it was originally decoded from a byte array using ASCII or UTF-8 to interpret the bytes.
还要注意,UTF-8与ASCII向后兼容,因为如果您有任何表示ASCII编码文本的有效字节序列,则相同字节序列应为用于表示UTF-8中的相同文本.
Also note that UTF-8 is backward-compatible with ASCII, in that if you've got any valid sequence of bytes representing ASCII-encoded text, that's the same sequence of bytes that would be used to represent the same text in UTF-8.
这篇关于如何在Java中将ASCII字符串转换为UTF-8字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!