美文网首页
Java byte[] 转 String的简单转换

Java byte[] 转 String的简单转换

作者: 夜瑾漠 | 来源:发表于2019-07-21 22:30 被阅读0次

简单的将byte[] 还原成了 String 值

[TOC]

代码:

public class Test0719 {
    public static void main(String[] args) {
        String text = "test";
        byte[] textBytes = text.getBytes();
        String content = byteToString(textBytes);
        System.out.println(textBytes + "\n" + content);
    }

    private static String byteToString(byte[] bytes) {
        if (null == bytes || bytes.length == 0) {
            return "";
        }
        String strContent = "";
        try {
            strContent = new String(bytes, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return strContent;
    }
}

运行结果:

运行结果.png

相关文章

网友评论

      本文标题:Java byte[] 转 String的简单转换

      本文链接:https://www.haomeiwen.com/subject/lpnelctx.html