-
-
Save rm77/cab5b7c94a8de22e6fb8f6f79c12405f to your computer and use it in GitHub Desktop.
Golang IP <-> int conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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