how to convert string to binary integer file using command line under linux
Question
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?
Solution 2
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
OTHER TIPS
A slightly simpler way would be:
printf "0: %08X" 1234 | xxd -r > int
this appears to produce the same output on my system. be sure to check perldoc -f pack
.
echo '1234' | perl -e 'print pack("nn", 0,<STDIN>);' > int
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow