[英]Java base64 encoding output from Apache Common the "wrong" result
我正在尝试使用 Apache Common 的 Base64 编码功能。 但我似乎得到了“错误”的结果。 文档在这里。
我的代码是这样的:
import org.apache.commons.codec.binary.Base64;
String data = "hi,all,how can this happen?";
byte[] databytes = Base64.encodeBase64(data.getBytes());
data = databytes.toString();
System.out.println(data);
//the result is:
//[B@121cc40
但是我使用 Python 编码相同的字符串,代码是:
import base64
print base64.b64encode("hi,all,how can this happen?")
#The result is aGksYWxsLGhvdyBjYW4gdGhpcyBoYXBwZW4/
怎么会有这种差别?
如果您想查看其内容,这不是在 Java 中打印字节(或任何其他类型)数组的方式。
这是:
System.out.println(Arrays.toString(data));
您正在打印地址。 如果要打印字符串数据,可以使用,
String password = new String(databytes);
System.out.println("Encoded String "+ password)
import org.apache.commons.codec.binary.Base64;
public class Codec {
public static void main(String[] args) {
try {
String data = "hi,all,how can this happen?";
String encodedText;
// Base64
encodedText = new String(Base64.encodeBase64(data.getBytes()));
System.out.println("Encoded: " + encodedText);
System.out.println("Decoded:"
+ new String(Base64.decodeBase64(encodedText.getBytes())));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
现在它将编码您的字符串数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.