```
package main
import (
"fmt"
"strconv"
"strings"
)
/* IP地址转化为二进制数 */
func main() {
addrInt := ipAddrToInt("192.168.8.152")
/* 十进制转化为二进制 */
c := strconv.FormatInt(addrInt, 2)
fmt.Println("c:", c)
/* 二进制转化为十进制 */
d, err := strconv.ParseInt(c, 2, 64)
fmt.Println("d:", d, err)
}
func ipAddrToInt(ipAddr string) int64 {
bits := strings.Split(ipAddr, ".")
b0, _ := strconv.Atoi(bits[0])
b1, _ := strconv.Atoi(bits[1])
b2, _ := strconv.Atoi(bits[2])
b3, _ := strconv.Atoi(bits[3])
var sum int64
sum += int64(b0) << 24
sum += int64(b1) << 16
sum += int64(b2) << 8
sum += int64(b3)
return sum
}
```
有疑问加站长微信联系(非本文作者))