Using ash, I have an IP address as a variable
IP_ADDR=192.168.1.234
I want to write the 4 bytes to a binary file and later reread them and reform the IP string.
I have the following working solution, but it seems very hacky - any better proposals?
Write:
IP_ADDR=192.168.1.234
serialHex=`printf '%02X' ${IP_ADDR//./ } | sed s/'\(..\)'/'\\\\x\1'/g`
echo -n -e $serialHex | dd bs=1 of=/path/to/file seek=19 &> /dev/null
Note seek=19 indicates where in the binary file (at byte 19) to write
Read:
hexValues=`od -j 19 --read-bytes=4 --address-radix=n -t x1 /path/to/file`
set $hexValues
for w; do echo -n "$((0x$w))."; done | sed s/.$//