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

时间:2016-11-03 15:41:10

标签: java encoding utf-8 bluetooth

实际上我处于这样一种情况,我需要读取一个utf8格式的字符串,但它的字符使用variable-length encoding所以我有问题将它们编码为字符串,并且在打印时我得到奇怪的字符,字符似乎是韩文,这是我使用的代码,但没有结果:

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);
}

我也使用了UTF-16并获得了更好的结果,但是它给了我奇怪的字符,根据上面提供的文档,我应该使用utf8。

提前感谢您的帮助。

修改

Base64值:S0QtOTI2IEdHMDA2AAAAAA == \ n enter image description here

2 个答案:

答案 0 :(得分:5)

蓝牙名称显示问题:

如果您检查蓝牙适配器setName(),您将获得

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

  

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

Android支持的版本:

如果您查看链接https://stackoverflow.com/a/7989085/2293534,您将获得Android支持的版本列表。

Supported and Non supported locales are given in the table:

-----------------------------------------------------------------------------------------------------
             | 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   |   -   |
-----------------------------------------------------------------------------------------------------

对于解决方案,

<强>溶液#1:

Michael为转化提供了一个很好的例子。有关更多信息,请查看https://stackoverflow.com/a/40070761/2293534

  

调用getBytes()时,您将获得字符串的原始字节   在您的系统的本机字符编码下编码(可能或   可能不是UTF-8)。然后,您将这些字节视为它们   用UTF-8编码,它们可能不是。

     

更可靠的方法是将ko_KR-euc文件读入   Java字符串。然后,使用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();
     

N.B:当然,您应该使用正确的编码名称

<强>溶液#2:

使用StringUtils,你可以做到 https://stackoverflow.com/a/30170431/2293534

<强>方案#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");

资源链接:

  1. Korean Codesets and Codeset Conversion
  2. Korean Localization
  3. Changing the Default Locale
  4. Byte Encodings and Strings

答案 1 :(得分:2)

我建议你为每个Apache库使用StringUtils。我相信你的必要方法记录在这里:

https://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/StringUtils.html