3

I have data in binary format (hex: 80 3b c8 87 0a 89) and I need to convert that into String in order to save binary data in MS Access db via Jackcess. I know, that I'm not suppose to use String in Java for binary data, however Access db is third party product and I have not control whatsoever.

So I tried to convert binary data and save it, but unfortunately the result was unexpected.

byte[] byteArray = new byte[] {0x80, 0x3b, 0xc8, 0x87, 0x0a 0x89};
System.out.println(String.format("%02X ",byteArray[0])+String.format("%02X ", byteArray[1]));//gives me the same values

String value = new String(byteArray, "UTF-8");//or any other encoding
System.out.println(value);//completely different values

I would like to know what going on under new String and if there is a way to convert binary data into String and have the same hex values.

Note 1: initially I read a binary file which has nothing to do with hex. I use hex just for comparison of datasets.

Note 2 There was a suggestion to use Base64 aka MIME, UTF-7, etc. By my understanding, it takes binary data and encodes that into ANSI charset, basically tweaking initial data. However,for me that is not a solution, because I must write exact data that I hold in binary array.

byte[] byteArray = new byte[]{0x2f, 0x7a, 0x2d, 0x28};
byte[]   bytesEncoded = Base64.encodeBase64(byteArray);
System.out.println("encoded value is " + new String(bytesEncoded ));//new data
4

2 回答 2

4

为了安全地将任意二进制数据转换为文本,您应该使用 hex 或 base64 之类的东西。UTF-8 等编码旨在将任意文本数据编码为字节,而不是将任意二进制数据编码为文本。就源数据而言,这是不同的。

我强烈建议为此使用库。例如,使用番石榴

String hex = BaseEncoding.base16().encode(byteArray);
// Store hex in the database in the text field...
...
// Get hex from the database from the text field...
byte[] binary = BaseEncoding.base16().decode(hex);

(当然也可以使用其他库,例如 Apache Commons Codec。)

或者,将二进制数据保存到 Access中专为二进制数据设计的字段中,而不是将其转换为文本。

于 2015-01-30T10:29:09.243 回答
1

要学习的基本课程 - 永远不要将二进制数据与等效的字符串混为一谈。

我的错误是,我将初始数据从 Access 导出到 csv,同时将索引字段的类型从二进制更改为字符串(现在我知道了,一团糟)。我来的解决方案 - 我自己的 Access 导出工具,所有数据都保存为二进制文件。感谢@gord-thompson - 他的评论导致了解决方案。

于 2015-02-07T12:25:11.440 回答