Java将字节数组转换为字符串UTF-8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24729951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:31:56  来源:igfitidea点击:

Java converting byte array to string UTF-8

javaandroidarraysbyte

提问by Phil

I'm trying to convert a byte array to a string. The problem is that UTF-8 won't work. I can't show "ü", "?" or something like this. Can anyone help me?

我正在尝试将字节数组转换为字符串。问题是 UTF-8 不起作用。我无法显示“ü”、“?” 或类似的东西。谁能帮我?

public static String byteToString(byte[] bytes) {
    String str = new String(bytes, Charsets.UTF_8);

    return str;
}

Would be nice if it works down until API 8 Android

如果它可以工作到 API 8 Android 会很好

回答by morgano

Try this instead:

试试这个:

String str = new String(bytes, Charset.forName("UTF8"));