Skip to content

Instantly share code, notes, and snippets.

@jeromesun14
Forked from jjarmoc/itoa.sh
Created April 23, 2021 08:45
Show Gist options
  • Select an option

  • Save jeromesun14/ec1b05a8e504db674d92f24bd5286c48 to your computer and use it in GitHub Desktop.

Select an option

Save jeromesun14/ec1b05a8e504db674d92f24bd5286c48 to your computer and use it in GitHub Desktop.

Revisions

  1. @jjarmoc jjarmoc created this gist Oct 19, 2011.
    29 changes: 29 additions & 0 deletions itoa.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    #Handy functions for .bashrc loading.
    #
    # $ atoi 192.168.1.1
    # 3232235777
    # $ itoa 3232235777
    # 192.168.1.1


    function atoi
    {
    #Returns the integer representation of an IP arg, passed in ascii dotted-decimal notation (x.x.x.x)
    IP=$1; IPNUM=0
    for (( i=0 ; i<4 ; ++i )); do
    ((IPNUM+=${IP%%.*}*$((256**$((3-${i}))))))
    IP=${IP#*.}
    done
    echo $IPNUM
    }

    function itoa
    {
    #returns the dotted-decimal ascii form of an IP arg passed in integer format
    echo -n $(($(($(($((${1}/256))/256))/256))%256)).
    echo -n $(($(($((${1}/256))/256))%256)).
    echo -n $(($((${1}/256))%256)).
    echo $((${1}%256))
    }