在Java中编码可变长度的utf8字节数组

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.

在此先感谢您的帮助.

编辑:

Base64值:S0QtOTI2IEdHMDA2AAAAAA == \n 在此输入图像描述

Sky*_*ker 5

蓝牙名称显示问题:

如果你检查蓝牙适配器setName(),你会得到它

https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#setName

使用UTF-8编码的有效蓝牙名称最多为248个字节,尽管许多远程设备只能显示前40个字符,有些可能仅限于20个字符.

Android支持的版本:

如果您查看链接/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.

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();
Run Code Online (Sandbox Code Playgroud)

注意:您当然应该使用正确的编码名称

解决方案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)

资源链接:

  1. 韩语代码集和代码集转换
  2. 韩国本地化
  3. 更改默认区域设置
  4. 字节编码和字符串