我有一个 UTF-8 编码的字符串“Château”,它被转换为 US-ASCII 格式作为“Ch??teau”(在我的应用程序的底层库中)
现在,我想从“U-ASCII”转换后的字符串“Ch??teau”中获取原始字符串“Château”。但是,我无法使用以下代码获得它。
StringBuilder masterBuffer = new StringBuilder();
byte[] rawDataBuffer = (Read from InputStream) // say here it is "Château"
String rawString = new String(rawDataBuffer, "UTF-8");
masterBuffer.append(rawString);
onMessageReceived(masterBuffer.toString().getBytes()) => Here, getBytes() uses the platform's default charset 'US-ASCII.
我的应用程序接收到 US-ASCII 编码的字节数组。 在应用程序方面,即使我尝试从中获取 UTF-8 字符串,也没有用。 转换尝试仍然给出“Ch??teau”。
String asciiString = "Ch??teau";
String originalString = new String(asciiString.getBytes("UTF-8"), "UTF-8");
System.out.println("orinalString: " + originalString);
“originalString”的值仍然是“Ch??teau”。
这是正确的方法吗?
谢谢,
最佳答案
你不能。您通过转换为 US-ASCII 丢失了信息。您无法找回丢失的东西。
关于java - 从 US-ASCII 编码字符串获取 UTF-8 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34045035/