Bash - 46
Table of content
You will find 4 differently golfed version:
echo $[_=32,`printf "%d<<(_-=8)|" ${1//./ }`0] # 46chr
set -- ${1//./ };echo $[$1<<24|$2<<16|$3<<8|$4] # 47chr
v=('|%d<<'{24,16,8,0});printf -vv "${v[*]}" ${1//./ };echo $[0$v] # 65chr
mapfile -td. i<<<$1;for((a=o=0;a<4;o+=i[a]<<(3-a++)*8)){ :;};echo $o # 68chr
New version! 2018-11-15 More golfed, 46 char
echo $[_=32,`printf "%d<<(_-=8)|" ${1//./ }`0]
Explanation
- I used
$_
for more golfing.
- Syntax
${1//./ }
will substitute every dots .
by spaces
.
- so
printf
will render something like 192<<(_-=8)|168<<(_-=8)|1<<(_-=8)|1<<(_-=8)|
- then we will add a
0
after last OR |
and
- preset
_
to 32. bash will read construct from left to right, so $((_-=8))
make 24
at 1st shift, 16
on second, and so on.
in action:
set -- 192.168.1.1
echo $[_=32,`printf "%d<<(_-=8)|" ${1//./ }`0]
3232235777
For fun: trying to get $_
content, after this:
echo $_
3232235777
;-b
set -- 192.168.1.1
echo $_ $[_=32,`printf "%d<<(_-=8)|" ${1//./ }`0] $_
192.168.1.1 3232235777 0
Ok, that's correct 32 - 4 x 8 = 0
In a function:
ip2int() {
echo $[_=32,`printf "%d<<(_-=8)|" ${1//./ }`0]
}
ip2int 192.168.1.1
3232235777
ip2int 255.255.255.255
4294967295
ip2int 0.0.0.0
0
or into a loop: -> 60
ip2int() {
for i;do
echo $[_=32,`printf "%d<<(_-=8)|" ${i//./ }`0]
done
}
ip2int 192.168.1.1 10.10.104.36 8.8.8.8 1.1.1.1 255.255.255.255 0.0.0.0
3232235777
168454180
134744072
16843009
4294967295
0
bash (v4.1+): 47
First post
set -- ${1//./ };echo $[$1<<24|$2<<16|$3<<8|$4]
Explanation:
- Syntax
${1//./ }
will substitute every dots .
by spaces
.
set --
set positional parameters ($@=($1 $2 $3...)
)
- So
set -- ${1//./ }
will split $1
by dots and set $1
, $2
, $3
and $4
if string containg 3
dots (and no spaces).
in action:
set -- 192.168.1.1
set -- ${1//./ };echo $[$1<<24|$2<<16|$3<<8|$4]
3232235777
or in a function:
ip2int() {
set -- ${1//./ }
echo $[$1<<24|$2<<16|$3<<8|$4]
}
ip2int 192.168.1.1
3232235777
ip2int 0.0.0.0
0
or into a loop: -> 61
for i;do set -- ${i//./ };echo $[$1<<24|$2<<16|$3<<8|$4];done
in action:
ip2int() {
for i;do
set -- ${i//./ }
echo $[$1<<24|$2<<16|$3<<8|$4]
done
}
ip2int 192.168.1.1 10.10.104.36 8.8.8.8 1.1.1.1 0.0.0.0
3232235777
168454180
134744072
16843009
0
Another version differently golfed: 65
v=('|%d<<'{24,16,8,0});printf -vv "${v[*]}" ${1//./ };echo $[0$v]
Sample:
ip2int() {
v=('|%d<<'{24,16,8,0});printf -vv "${v[*]}" ${1//./ };echo $[0$v]
}
ip2int 255.255.255.255
4294967295
ip2int 10.10.104.36
168454180
In a loop (+14): 82
ip2int() {
for i;do
v=('|%d<<'{24,16,8,0})
printf -vv "${v[*]}" ${1//./ }
echo $[0$v]
done
}
* or a little more ugly: 70*
v=('|%d<<'{24,16,8});printf -vv "${v[*]}" ${1//./ };echo $[0${v%<<2*}]
where printf
give some string like |192<<24 |168<<16 |1<<8|1<<24 |0<<16 |0<<8
we have to cut at last <<2...
.
golfed with mapfile
, longer: 68
ip2int() {
mapfile -td. i<<<$1;for((a=o=0;a<4;o+=i[a]<<(3-a++)*8)){ :;};echo $o
}
or with loop: 82
ip2int() {
for a;do
mapfile -td. i<<<$a;for((a=o=0;a<4;o+=i[a]<<(3-a++)*8)){ :;};echo $o
done
}