I would like to create a file that just contains a binary number. I think that touch can be used to create an empty file, but is there any way I can fill it with a binary number e.g. 10 (ten)? And how can I validate that the file contains the binary value of ten?

See also How can I check the Base64 value for an integer?


Convert the number to hex (in this case A) and then do:

echo -en '\xA' > file
| improve this answer | |
  • Thanks, however, now I don't know how I can validate this anser. E.g could I write the number 65 and in some way print that with a command? – Jonas Oct 24 '10 at 15:41
  • 1
    @Jonas: hexdump file will display the bytes in a file as hex. – sepp2k Oct 24 '10 at 15:42
  • Thanks for hexdump. I added that to my question. However I don't think echoing the hexnumber is working. When I look at the file with hexdump file I get 0000000 785c 0041 0000003 and I get a different value for echo -n '\xa > file` – Jonas Oct 24 '10 at 15:55
  • The file seems to contain something more than a number, and with hexdump should the hexvalue \xA be shown as A or a? But it doesn't in this case. – Jonas Oct 24 '10 at 15:58
  • 1
    @Jonas: Ok, my bad. I tested this in zsh and apparently zsh's echo behaves differently than bash's. In bash you need the -e option to echo for \x to work. – sepp2k Oct 24 '10 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.