我想把一个文件(.jpg图片文件)转换成一个txt文件(ASCII码)
现在,我有两个计划,将文件字节转换为 Hex 和 Base64
File file=new File("D:/temp/1.jpg");
byte[] bytes=getBytesFromFile(file);
System.out.println("bytes:"+bytes.length);//bytes:9006
String hex=HexUtil.asHex(bytes);
String base64=Base64.encode(bytes);
System.out.println("hex:"+hex.length());//hex:18012
System.out.println("base64:"+base64.length());//base64:12165
我认为 Base64 更好,因为发送 12165 比十六进制字符串少。
我想知道,还有什么好的办法吗?
非常感谢! :)
最佳答案
Base64 或多或少是将二进制信息作为字符串传输的标准方式。所以是的,你做对了。维基百科:
Base64 encoding schemes are commonly used when there is a need to encode binary data that needs be stored and transferred over media that are designed to deal with textual data
在我的实践中,当通过网络服务发送附件时,我用它来传输它们。 (这是政府规定的(该项目是关于电子政务的))
关于java - 将二进制数据转换为字符数据的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4276721/