在Java中将byte [] salt转换为String [英] Convert byte[] salt to String in java

查看:134
本文介绍了在Java中将byte [] salt转换为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将byte []格式的生成盐值转换为String。
但是每次转换为特殊字符。

I am trying to convert the generate salt value which is in the byte[] format to String. But each time it converts in to special characters.

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

I used following way for converting byte[] to 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 方法

you can convert using base64 encoding. to do this you can use javax.xml.bind.DatatypeConverter.printBase64Binary method

例如:

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

我也想添加 answer


byte []和String之间的正确转换 b说明要使用的编码。如果你从一个byte []开始,它
实际上不包含文本数据,没有正确的转换。
字符串是用于文本,byte []是用于二进制数据,唯一真正的
是合理的做法是避免它们之间的转换,除非你
绝对必须。

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中将byte [] salt转换为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆