using bash: write bit representation of integer to file
printf
is more portable than echo
. This function takes a decimal integer and outputs a byte with that value:
echobyte () { if (( $1 >= 0 && $1 <= 255 )) then printf "\\x$(printf "%x" $1)" else printf "Invalid value\n" >&2 return 1 fi}$ echobyte 97a$ for i in {0..15}; do echobyte $i; done | hd00000000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f |................|00000010
You can use echo to emit specific bytes using hex or octal. For example:
echo -n -e \\x30
will print ascii 0 (0x30)
(-n remove trailing newline)
xxd
is the better way. xxd -r infile outfile
will take ascii hex-value in infile to patch outfile, and you can specify the specific position in infile by this: 1FE:55AA