How to convert string to binary integer 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 named int, containing a 32-bit integer with a large number, with a value of 1234.

The only way I decided to do this is something like

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

which is a little nasty!

Does anyone know a better way?

+3
source share
3 answers

ok seeing well that the williams sign seems to have passed awol, I will post a revised version of his answer

echo 1234 | perl -e 'print pack("N", <STDIN>); > int
+3
source

Somewhat simpler:

printf "0: %08X" 1234 | xxd -r > int
+5
source

, . perldoc -f pack.

echo '1234' | perl -e 'print pack("nn", 0,<STDIN>);' > int

0

All Articles