I've seen lots of somewhat related questions and answers on this forum, but haven't found anything that addresses my problem.
The idea is simple enough:
(This is using the Java programming language - I am currently restricted to using Java 7)
An array of bytes (representing anything: some wire format, some form of encoded data, binary data with embedded "text", etc.) is received. I'd like to be able to print the array in the following forms:
- as a hexadecimal string
- as "printable" text
The first case is partly for debugging reasons, but also may have use in a non-debugging mode. The second case is purely for debugging reasons, and would allow for human comparison with other sources of information.
I think I have a solution for the first case, but am struggling with the second case.
Obviously an array of bytes may contain non-printable characters or characters that are rendered in different ways. How can I "print" an array so that all bytes are represented? A good analogue I can provide is the UNIX od
command, where od -ah
displays the binary data in a hexadecimal form as well as ASCII characters. In this case dots (.
) are typically used in place of non-printable or control characters.
I don't need it to specifically look like what od
puts out, but would like to be able to show the data so that at least the printable characters can be seen and the rest represented with some sort of placeholder. Also, I don't want to remove the non-printable characters, as that would give a misleading representation of the data.
If anyone has information on how to achieve this, I would really appreciate it.