问题
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.
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);
}
回答1:
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");
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.
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");
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.
回答2:
I propose the following solution:
NSString *line_utf16[ENOUGH_MEMORY_SIZE];
line_utf16= [NSString stringWithFormat: @"%s", line_utf8];
ENOUGH_MEMORY_SIZE is at least twice exceeds memory used for line_utf8
I suppose memory for line_utf16 has to be dynamically or statically allocated at least twice of the size of line_utf8.
If you run into similar problem please add a couple of sentences!
来源:https://stackoverflow.com/questions/13412174/how-to-convert-utf8-string-to-utf16