tclhexdumpxxd

Unix command/s or Tcl proc to convert hex data to binary with output 1 bit per line


I have a plain text file with hex data information (32-bit word per line). Example :

cafef00d
deadbeef

That I need to convert to this :

11001010111111101111000000001101
11011110101011011011111011101111

BUT with 1 bit per line only. Starting from the LSB of the first 32-bit hex and so on. Final output file will be :

1
0
1
1
... and so on

Is there a unix command/s or can I do this in a Tcl proc ?


Solution

  • A solution...

    Assuming you've read the file into a string, the first thing is to convert the hex strings into numbers expressed in binary, LSB first. There's a few ways to do it, here's one (I like scan and format):

    set binaryData [lmap hexValue $inputData {
        scan $hexValue "%x" value
        string reverse [format "%b" $value]
    }]
    

    For your input, that produces:

    10110000000011110111111101010011 11110111011111011011010101111011
    

    We can then convert that to be one digit per line with this:

    set oneDigitPerLine [join [split [join $binaryData ""] ""] "\n"]
    

    The innermost join gets rid of the whitespace, the split breaks it up into characters, and the outer join inserts the newline separators. (I'll not produce the result here.)