3

I wrote something that I think parses bits from a file, but now I need to test it. I'd like to create a file that I know has bits in a certain order. Something like:

000010000000000000000000000100000

Then I can use this file to test by bit parser, so I can tell it to grab bits 0-4 (ordered left to right) and expect it to return 00001 and then grab bits 26-31 and expect it to return 100000

How can I create a file like this in osx?

1
  • 1
    use a Hex editor to create the file. that way you get actual binary data instead of encoded text that consists of 1 and 0 characters. a web search for osx hex editor turns up quite a few options. Depending on your choice of products, you may have to enter the data in Hex, but that will still result in a binary file. Also, I would recommend that in general you perform your operations in bytes rather than nibbles, as you will have more compability with existing program runtimes and langague features. Dec 4, 2015 at 23:04

1 Answer 1

4

You can use the printf command for it.

To get your example 0000 1000 0000 0000 0000 0000 0001 0000 do:

$ printf "\x08\x00\x00\x10" > file1
$ hexdump file1
00000000  08 00 00 10                                   |....|
00000004

Note that your example is 33 bits, I've used the first 32 bits of it.

If you want a bit more variation, try:

$ echo -n "aC3@hLp-" > file2
$ hexdump -C file2
00000000  61 43 33 40 68 4c 70 2d                           |aC3@hLp-|
00000008

Every file is a 'binary' file. The only difference between, for example, an ASCII text file and a binary file is that the range of the values of the bytes in a text file are limited to the ASCII characters. The same is true for any other encoding (e.g. Unicode UTF-8 or UTF-16).

You must log in to answer this question.