Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert string to binary integer file using command line under linux

What i want is to take an integer represented as a string, for example "1234", and convert it to a file called int, containing a 32-bit big endian integer, with the value 1234.

The only way I have figured out to do this is something like

echo 1234 | awk '{printf "0: %08X", $1}' | xxd -r > int

which is a bit nasty!

Does anyone know a better way?

like image 982
Patrick_O Avatar asked Nov 28 '25 22:11

Patrick_O


2 Answers

A slightly simpler way would be:

printf "0: %08X" 1234 | xxd -r > int
like image 71
Paige Ruten Avatar answered Nov 30 '25 11:11

Paige Ruten


ok well seeing that mark williams seems to have gone awol i will post the corrected version of his answer

echo 1234 | perl -e 'print pack("N", <STDIN>); > int
like image 22
Patrick_O Avatar answered Nov 30 '25 12:11

Patrick_O



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!