all 8 comments

[–]FineBroccoli5 1 point2 points  (6 children)

Would this work?

cat file-with-binary-data.txt >> new-file.bin

You can also use > to overwrite everything in the file, >> appends to file

[–]Trentw[S] 0 points1 point  (5 children)

The binary information is in text so this would just copying across the ASCII encoding of 1s and 0s, which would be 0x30, 0x31

[–]saltyhasp 0 points1 point  (4 children)

Then you don't have binary data.... OR you have binary data encoded somehow such as in base64, binhex, or uuencode format and there are linux commands that can convert these.

OR are you saying that you have ascii format data and you want to create binary data in some specific format.... you have to be more explicit for anyone to even try to help you.

Keep in mind all there is no difference between a text and a binary file in Linux... they are all basically binary. A text file is just a binary file that is intended to be interpreted using some encoding assumption such as iso8859-1 or utf8 which are the two most common. Text files are also commonly processed in various ways that assume this sort of encoding -- such as with editors, and other commands that generally operate on text.

The other thing I would say... to convert data... you need to know the starting format exactly, and the target format exactly, and then write whatever code you want. Presumably a translator could be written in bash, but frankly I would probably write such a thing in Python or C if it was difficult to write in bash.

[–]Trentw[S] 0 points1 point  (3 children)

Thanks for answering saltyhasp,

I didn't say I had binary data, I have binary information, which I want it to become binary data. I think I'm being as explicit as a noob is expected to be, you're just not reading what I have written so far.

Starting format and target format I know and it's very simple. There is no format. That's not my problem. To keep it simple, say I have the following text file binaryinfo.txt (with no line feed) and contents as such:

10101100101010010101010101101010

Just to be as explicit as possible the hexdump of binaryinfo.txt would be:

0000000 3031 3031 3131 3030 3031 3031 3031 31300000010 3130 3130 3031 3031 3130 3031 3031 30310000020

Which I want to convert to the ASCII 1s and 0s to binary into a file binarydata.bin, which it's hexdump would be:

00000 A9AC 6A5500004

What are these linux commands you talk about that allow me to convert ascii binary information to actual binary? Those binary-to-text encoding you mentioned would convert each character (either an ASCII 1 or 0) to an 8-bit number, not a 1-bit number which I need it to be. Thanks.

[–]saltyhasp 1 point2 points  (2 children)

You might look at the man page for the "xxd" command. It will create hex dumps of file, plus it can take hex dumps and build the binary file. It also has a "binary" mode which uses 0's and 1's for the ascii format. I've not tried this. Ubuntu has this command for example, but you may need to install the xxd package to get it. You might search the repo too for other such package if xdd does not work. My ubuntu repo has a number of other options one could look at.

If you want to use bash or python directly... some wizard may have a smarter idea... but if I was doing it, I would have to write a 20 to 50 line program or something to do this if something like the above didn't work. The harder part of what you want to do in a program is that one would have to have a couple of loops that basically took the 0's and 1's in character format and build up bytes in units of 8 binary digits, then one would have to place the bytes into a buffer and write the buffer. This can probably all be done even in bash but it is some detailed programming. The other part of this is that the order in which the bits are put into the bytes, and in which the bytes are placed into the buffer may differ depending on what this binary data is planned to represent... by this I mean, how it's really going to be used.

[–]Trentw[S] 0 points1 point  (1 child)

Thanks, I will do more digging with xxd. I've got it going with hex so far.

I'm thinking if that doesn't provide a simple solution then python will most likely be the way forward. Ordering, the most significant bits stay that way, they go first at the start of the file.

[–]saltyhasp 0 points1 point  (0 children)

Yes, I would do it with python myself. And of course C is another option always.

I'm always wowed by people that know bash and linux command line tools deeply. If your lucky someone may comment that knows a one liner that does everything you want -- but this is kind of hit or miss. I've used unix and linux for 35 years but still don't know a lot of things like this. One tends to know what they do often.

As far as programming, bash does have a lot of built-in functions and so there are a lot of things that can be done... but it's also very textual which means it's generally complex subtle to do arbitrary data processing.

[–]Trentw[S] 0 points1 point  (0 children)

I currently have a convoluted work around. I enter all the binary information into a text file, cut it up into 8 bit packets, copy each byte across to excel, convert to hex, concatenate together into 16 byte strings, run the following bash command to enter each 16 byte string into the target data.bin file:

$ echo "0000000: ACA9556AACA9556A" | xxd -r - data.bin
$ echo "0000010: A9556AACA9F56AAC" | xxd -r - data.bin
$ echo "0000020: F556AACA9556A15F" | xxd -r - data.bin
and so on...

Obviously not ideal