开发者

Encode binary data as ASCII in Java

开发者 https://www.devze.com 2023-04-05 20:25 出处:网络
I have a bitset of binary data that I wish to encode compactly as an ASCII string.I intend to initially compress the data using run-length encoding to give a sequence of integers; e.g.

I have a bitset of binary data that I wish to encode compactly as an ASCII string. I intend to initially compress the data using run-length encoding to give a sequence of integers; e.g.

111110001000000000000111

becomes:

5o3z1o12z3o

(e.g. 5 ones开发者_如何学JAVA, 3 zeros, 1 one, 12 zeros, 3 ones).

However, I wish to then compress this further into a compact ASCII string (i.e. a string using the full range of ASCII characters rather than the digits plus 'o' and 'z'). Can anyone recommend a suitable approach and / or 3rd party library to do this in Java?


If your goal is compression, just gzip the stream. It's going to do better than your run-length encoding.

Then if you need it to be text for some reason, like to safely pass through old mail gateways, I'd also turn to a standard encoding like Base64, rather than make up your own.

But if you want to roll your own: first I'd note that you don't need the 'o' and 'z'. You already know those values since they alternate. Assume it starts on 0 (and if it doesn't, encode an initial 0 to show that there are 0 0s).

Encoding the numbers textually is possible but probably inefficient. Look into a variable-length encoding for integer values, then encode those bytes. Then 'escape' them into ASCII somehow.

But then we're back to Base64-like encoding, and the first suggestion to gzip + Base64 is probably easier than all of this.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号