need a shell script to convert big endian to little endian need a shell script to convert big endian to little endian shell shell

need a shell script to convert big endian to little endian


For 32 bit addresses, assuming it's zero padded:

v=00d66d7e echo ${v:6:2}${v:4:2}${v:2:2}${v:0:2}# 7e6dd600


Based on Karoly's answer you could use following script, reading piped input:

#!/bin/bash# check stdinif [ -t 0 ]; then exit; fiv=`cat /dev/stdin`i=${#v}while [ $i -gt 0 ]do    i=$[$i-2]    echo -n ${v:$i:2}doneecho

For e.g. you could save this script as endian.sh and make it executable with:

chmod u+x endian.sh

Then:

echo 00d66d7e | ./endian.sh

gives you:

7e6dd600

For a different length string:

echo d76f411475428afc90947ee320 | ./endian.sh

result would be:

20e37e9490fc8a427514416fd7


Just had to do this... but from decimal to little endian.. adapting that here:

echo 00d66d7e | tac -rs .. | echo "$(tr -d '\n')"

achieves the desired result, for arbitrarily sized hexadecimal representations of unsigned integers.

(h/t 'tac -rs' MestreLion, very nice!)