问题描述
我通过处理客户端应用程序发送的请求来获取 UTF8 字符串.但是字符串确实是UTF16.我该怎么做才能将它放入我的本地字符串中是一个字母后跟 \0
字符?我需要将该字符串转换为 UTF16.
I'm getting a UTF8 string by processing a request sent by a client application. But the string is really UTF16. What can I do to get it into my local string is a letter followed by \0
character? I need to convert that String into UTF16.
接收字符串示例:S\0a\0m\0p\0l\0e
(UTF8).
我想要的是:Sample
(UTF16)
Sample received string: S\0a\0m\0p\0l\0e
(UTF8).
What I want is : Sample
(UTF16)
FileItem item = (FileItem) iter.next();
String field = "";
String value = "";
if (item.isFormField()) {
try{
value=item.getString();
System.out.println("====" + value);
}
推荐答案
如果来自服务器的字节看起来像 S\0a\0m\0p\0l,则它们不是 UTF-8\0e
.它们是 UTF-16.您可以使用以下命令将 UTF16 字节转换为 Java String
:
The bytes from the server are not UTF-8 if they look like S\0a\0m\0p\0l\0e
. They are UTF-16. You can convert UTF16 bytes to a Java String
with:
byte[] bytes = ...
String string = new String(bytes, "UTF-16");
或者,如果您知道来自服务器的字节流的字节序,您可以使用 UTF-16LE
或 UTF-16BE
作为字符集名称.
Or you can use UTF-16LE
or UTF-16BE
as the character set name if you know the endian-ness of the byte stream coming from the server.
如果您已经(错误地)从字节中构造了一个 String
,就好像它是 UTF-8,您可以使用以下命令转换为 UTF-16:
If you've already (mistakenly) constructed a String
from the bytes as if it were UTF-8, you can convert to UTF-16 with:
string = new String(string.getBytes("UTF-8"), "UTF-16");
然而,正如 JB Nizet 指出的那样,如果字节不是有效的 UTF-8,则此往返(字节 -> UTF-8 字符串 -> 字节)可能会造成损失.
However, as JB Nizet points out, this round trip (bytes -> UTF-8 string -> bytes) is potentially lossy if the bytes weren't valid UTF-8 to start with.
这篇关于如何将 UTF8 字符串转换为 UTF16的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!