Skip to content

Instantly share code, notes, and snippets.

@kevsersrca
Forked from ammario/ipint.go
Created March 23, 2018 12:56
Show Gist options
  • Save kevsersrca/bba4b2234a355e19036d503bc5cc0f4e to your computer and use it in GitHub Desktop.
Save kevsersrca/bba4b2234a355e19036d503bc5cc0f4e to your computer and use it in GitHub Desktop.
Golang ip <-> int conversion
func ip2int(ip net.IP) uint32 {
if len(ip) == 16 {
return binary.BigEndian.Uint32(ip[12:16])
}
return binary.BigEndian.Uint32(ip)
}
func int2ip(nn uint32) net.IP {
ip := make(net.IP, 4)
binary.BigEndian.PutUint32(ip, nn)
return ip
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment