I have a text file with two (2)
only possible characters (and maybe new lines \n
). Example:
ABBBAAAABBBBBABBABBBABBB
(Size 24 bytes
)
How can I convert this to a binary file, meaning a bit representation, with each one of the two possible values being assigned to 0
or 1
?
Resulting binary file (0=A
, 1=B
):
011100001111101101110111 # 24 bits - not 24 ASCII characters
Resulting file in Hex:
70FB77 # 3 bytes - not 6 ASCII characters
I would be mostly interested in a command-line solution (maybe dd
,xxd
, od
, tr
, printf
, bc
). Also, regarding the inverse: how to get back the original?
Best Answer
Another perl:
Proof:
The above reads input one line at a time. It's up to you to make sure the lines are exactly what they are supposed to be.
Edit: The reverse operation:
This reads a byte of input at a time.
Edit 2: Simpler reverse operation:
The above reads 3 bytes at a time from
STDIN
(but receivingEOF
in the middle of a sequence is not a fatal problem).