views:

19

answers:

1

Hi,

the ifconfig output of my machine is the following:

:~ shell$ ifconfig en0
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 00:25:bc:e7:39:6a 
    inet6 fe80::225:bcff:fee7:396a%en0 prefixlen 64 scopeid 0x4 
    inet 10.170.133.45 netmask 0xffffff00 broadcast 10.170.133.255
    media: autoselect (100baseTX <full-duplex>)
    status: active

I know that netmask 0xffffff00 is the hexadecimal representation of 255.255.255.0.

But which is the correct way of converting that string into the octet format?

Can you suggest some unix command or shell script?

+1  A: 

say you already gotten that hex number into a variable

$ s="0xffffff00"
$ p="${s:0:2}"
$ printf "%d.%d.%d.%d\n" ${p}${s:2:2} ${p}${s:4:2} ${p}${s:6:2} ${p}${s:8:2}
255.255.255.0
ghostdog74