简体   繁体   English

如何将字节数组转换为字符串?

[英]How to convert a byte array to a string?

How do I convert a byte array to a string? 如何将字节数组转换为字符串? Is this the correct syntax? 这是正确的语法吗?

byteArray.toString();

您需要提供一种编码,否则您将获得系统默认编码,本质上是一个随机值。

String value = new String(byteArray, encoding);

The Best way to convert bytearray to String is 将bytearray转换为String的最佳方法是

public void convertByteArrayToString(Charset encoding) {

        byte[] byteArray = new byte[] {87, 79, 87, 46, 46, 46};

        String value = new String(byteArray, encoding);

        System.out.println(value);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM