Go语言版的ip2long long2ip
ip2long
func Ip2long(ipstr string) (ip uint32) { r := `^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})` reg, err := regexp.Compile(r) if err != nil { return } ips := reg.FindStringSubmatch(ipstr) if ips == nil { return } ip1, _ := strconv.Atoi(ips[1]) ip2, _ := strconv.Atoi(ips[2]) ip3, _ := strconv.Atoi(ips[3]) ip4, _ := strconv.Atoi(ips[4]) if ip1>255 || ip2>255 || ip3>255 || ip4 > 255 { return } ip += uint32(ip1 * 0x1000000) ip += uint32(ip2 * 0x10000) ip += uint32(ip3 * 0x100) ip += uint32(ip4) return }
long2ip
func Long2ip(ip uint32) string { return fmt.Sprintf("%d.%d.%d.%d", ip>>24, ip<<8>>24, ip<<16>>24, ip<<24>>24) }