Converting a file from ASCII to Binary

26,667

Please note, that on the file system level, there is no difference between ascii and binary files. An ascii or text file is just a binary file containing bytes that are human readable (or control commands like LF=new line).

To display the stored bytes in binary form, you can use xxd (part of vim):

xxd -b INPUTFILE | cut -d" " -f 2-7 | tr "\n" " "

To store the result in a file, use:

xxd -b INPUTFILE | cut -d" " -f 2-7 | tr "\n" " " > OUTPUTFILE
Share:
26,667

Related videos on Youtube

mlegge
Author by

mlegge

Updated on November 27, 2022

Comments

  • mlegge
    mlegge over 1 year

    I have a series of files that are either delimited and fixed width, containing many rows of both character and numeric values like so (delimited example):

    Smith,Audrey,Ford,2000,21300.99
    Miller,Heath,Dodge,1990,2000.99
    Miller,Heath,Dodge,2010,15200.99
    

    How can I convert this into the binary file:

    01010011 01101101 01101001 01110100 01101000 00101100 01000001 01110101 01100100 01110010 01100101 01111001 00101100 01000110 01101111 01110010 01100100 00101100 00110010 00110000 00110000 00110000 00101100 00110010 00110001 00110011 00110000 00110000 00101110 00111001 00111001 00001010 01001101 01101001 01101100 01101100 01100101 01110010 00101100 01001000 01100101 01100001 01110100 01101000 00101100 01000100 01101111 01100100 01100111 01100101 00101100 00110001 00111001 00111001 00110000 00101100 00110010 00110000 00110000 00110000 00101110 00111001 00111001 00001010 01001101 01101001 01101100 01101100 01100101 01110010 00101100 01001000 01100101 01100001 01110100 01101000 00101100 01000100 01101111 01100100 01100111 01100101 00101100 00110010 00110000 00110001 00110000 00101100 00110001 00110101 00110010 00110000 00110000 00101110 00111001 00111001
    

    from the command line? Would it be different for delimited or fixed width files?

    A similar question asked was Convert file with integers written in ascii to binary file of integers, but this does not work for mixed type and multiple entry rows.

    I am on a x86_64 GNU/Linux (little endian) machine.

    EDIT:

    Is there a simple way to actually encode the ASCII text file to binary instead of just viewing the human readable version?

  • syntaxerror
    syntaxerror over 9 years
    @jofel It is a matter of definition what "ascii" is. There exists indeed a hybrid kind of files called "ascii hex", which are text files with e. g. readable "literal" ff af 18 26 ... the first of which, in the source file, is NOT 0xff but two times small f - hence "literal". I did that myself in the past: read in 'ff' as-is (or var="ff"), but then printf "\xff" (resp. printf "\x$var") as hex, >>-appended to output file. For instance, if you use hdparm --read-sector..., and redirect the output to a file, the resulting file will be "ascii hex" as well, no "true hex" yet.
  • YoloTats.com
    YoloTats.com over 9 years
    @syntaxerror Thanks for this addition. You get this "ascii-hex" files if you store (parts of) the output of any hexdump program into a file.
  • syntaxerror
    syntaxerror over 9 years
    You're welcome. And yes, that is pretty common practice. Unfortunately (especially with hdparm) you sometimes may have a jolly hard time to get this "fake hex" back to true hex to obtain a true binary data input file. Cf. also this question which is about said problem.