在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
原文链接: https://studygolang.com/articles/11612 https://blog.csdn.net/yzf279533105/article/details/81238758 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 } func UInt32ToIP(intIP uint32) net.IP { var bytes [4]byte bytes[0] = byte(intIP & 0xFF) bytes[1] = byte((intIP >> 8) & 0xFF) bytes[2] = byte((intIP >> 16) & 0xFF) bytes[3] = byte((intIP >> 24) & 0xFF) return net.IPv4(bytes[3], bytes[2], bytes[1], bytes[0]) }
|
请发表评论