gpt4 book ai didi

java - 在 java 中将 byte[] salt 转换为 String

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:49:50 27 4
gpt4 key购买 nike

我正在尝试将 byte[] 格式的生成盐值转换为字符串。但是每次它都会转换为特殊字符。

我使用以下方式将 byte[] 转换为 String :

byte[] salt = new byte[] { 50, 111, 8, 53, 86, 35, -19, -47 };
String saltString = new String(salt,"UTF-8");
System.out.println(saltString);

但是输出像 ??@?

最佳答案

您可以使用 base64 编码进行转换。为此,您可以使用 javax.xml.bind.DatatypeConverter.printBase64Binary方法

例如:

    byte[] salt = new byte[] { 50, 111, 8, 53, 86, 35, -19, -47 };
System.out.println(DatatypeConverter.printBase64Binary(salt));

我还想添加这个 answer

The "proper conversion" between byte[] and String is to explicitly state the encoding you want to use. If you start with a byte[] and it does not in fact contain text data, there is no "proper conversion". Strings are for text, byte[] is for binary data, and the only really sensible thing to do is to avoid converting between them unless you absolutely have to.

关于java - 在 java 中将 byte[] salt 转换为 String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16853957/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com