M. *_*aei 9 java encoding bluetooth utf-8
实际上,我需要读取一个utf8格式的字符串,但它的字符使用可变长度编码,所以我有问题将它们编码为字符串,打印时我得到奇怪的字符,字符似乎是韩文这是我使用的代码,但没有结果:
public static String byteToUTF8(byte[] bytes) {
try {
return (new String(bytes, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Charset UTF8_CHARSET = Charset.forName("UTF-8");
return new String(bytes, UTF8_CHARSET);
}
Run Code Online (Sandbox Code Playgroud)
我也使用了UTF-16并获得了更好的结果,但是它给了我奇怪的字符,根据上面提供的文档,我应该使用utf8.
在此先感谢您的帮助.
编辑:
如果你检查蓝牙适配器setName(),你会得到它
https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#setName
使用UTF-8编码的有效蓝牙名称最多为248个字节,尽管许多远程设备只能显示前40个字符,有些可能仅限于20个字符.
如果您查看链接/sf/answers/559235981/,您将获得Android支持的版本列表.
-----------------------------------------------------------------------------------------------------
| DEC Korean | Korean EUC | ISO-2022-KR | KSC5601/cp949 | UCS-2/UTF-16 | UCS-4 | UTF-8 |
-----------------------------------------------------------------------------------------------------
DEC Korean | - | Y | N | Y | Y | Y | Y |
-----------------------------------------------------------------------------------------------------
Korean EUC | Y | - | Y | N | N | N | N |
-----------------------------------------------------------------------------------------------------
ISO-2022-KR | N | Y | - | Y | N | N | N |
-----------------------------------------------------------------------------------------------------
KSC5601/cp949| Y | N | Y | - | Y | Y | Y |
-----------------------------------------------------------------------------------------------------
UCS-2/UTF-16| Y | N | N | Y | - | Y | Y |
-----------------------------------------------------------------------------------------------------
UCS-4 | Y | N | N | Y | Y | - | Y |
-----------------------------------------------------------------------------------------------------
UTF-8 | Y | N | N | Y | Y | Y | - |
-----------------------------------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
解决方案1:
迈克尔为转换提供了一个很好的例子.有关更多信息,请查看/sf/answers/2804953301/
当您调用getBytes()时,您将获得在系统的本机字符编码(可能是也可能不是UTF-8)下编码的字符串的原始字节.然后,您将这些字节视为UTF-8编码,它们可能不是.
更可靠的方法是将ko_KR-euc文件读入Java String.然后,使用UTF-8编码写出Java String.
Run Code Online (Sandbox Code Playgroud)InputStream in = ... Reader reader = new InputStreamReader(in, "ko_KR-euc"); // you can use specific korean locale here StringBuilder sb = new StringBuilder(); int read; while ((read = reader.read()) != -1){ sb.append((char)read); } reader.close(); String string = sb.toString(); OutputStream out = ... Writer writer = new OutputStreamWriter(out, "UTF-8"); writer.write(string); writer.close();注意:您当然应该使用正确的编码名称
解决方案2:
使用StringUtils,你可以做到这一点 /sf/answers/2111930201/
解决方案#3:
您可以使用Apache Commons IO进行转换.这里给出了一个非常好的例子:http://www.utdallas.edu/~lmorenoc/research/icse2015/commons-io-2.4/examples/toString_49.html
1 String resource;
2 //getClass().getResourceAsStream(resource) -> the <code>InputStream</code> to read from
3 //"UTF-8" -> the encoding to use, null means platform default
4 IOUtils.toString(getClass().getResourceAsStream(resource),"UTF-8");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1029 次 |
| 最近记录: |