linux - Sh, awk: how to convert integers, visible in the shell as a string to 16-bit low-/big- endian binary integers? -
i have bash sequence,
grep "integer =" $1 | awk -f= '{printf("%d\n",int($2*327))}'
which filters producing smth like:
6768 6572 6638 8403 8436 8436 8305 8502
i need these numbers placed in binary file 16-bit low-endian words (or big-endian if specified). there awk-, bash- way it?
ideally, like:
grep "integer =" $1 | awk -f='{to16bit_lendian(printf("%d\n",int($2*327)))}' >> out.bin
this should work:
cat $1 | grep "integer =" | awk -f=' function out(b) { if(b==0) { system("printf \"\\00\""); } else { printf("%c",b); } } function shorttole(n) { n%=65536; msb=n/256; lsb=n%256; out(lsb); out(msb); } { shorttole($2*327) } ' >> out.bin
and optimized way removing useless cat , grep:
awk -f" =" ' function out(b) { if(b==0) { system("printf \"\\00\""); } else { printf("%c",b); } } function shorttole(n) { n%=65536; msb=n/256; lsb=n%256; out(lsb); out(msb); } $1 == "integer" { shorttole($2*327) } ' $1 >> out.bin
Comments
Post a Comment