views:

1572

answers:

3

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?

+4  A: 

A slightly simpler way would be:

printf "0: %08X" 1234 | xxd -r > int
yjerem
A: 

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

only works for numbers up to 65535
Patrick_O
so what I really want is pack("N",<STDIN>)
Patrick_O
yeah, glad you caught my mistake and sorry i didn't take my own advice and consult perldoc first.
if you update your answer i will mark it as accepted.
Patrick_O
+2  A: 

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
Patrick_O