How to write binary data in Bash

前端 未结 6 2047
后悔当初
后悔当初 2021-01-20 05:54

Let us say there is a variable in bash script with value \"001\" how can i write this binary data into a file as bits (as \"001\" not \"1\") echo writes it as string but i w

6条回答
  •  野的像风
    2021-01-20 06:38

    A couple of more general functions to output integers to a file:

    le16 () { # little endian 16 bit;  1st param: integer to 2nd param: file 
      v=`awk -v n=$1 'BEGIN{printf "%04X", n;}'`
      echo -n -e "\\x${v:2:2}\\x${v:0:2}" >> $2
    }
    
    le32 () { # 32 bit version
      v=`awk -v n=$1 'BEGIN{printf "%08X", n;}'`
      echo -n -e "\\x${v:6:2}\\x${v:4:2}\\x${v:2:2}\\x${v:0:2}" >> $2
    }
    

提交回复
热议问题