Skip to content

Instantly share code, notes, and snippets.

@ammario
Last active April 20, 2024 12:51
Show Gist options
  • Star 73 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save ammario/649d4c0da650162efd404af23e25b86b to your computer and use it in GitHub Desktop.
Save ammario/649d4c0da650162efd404af23e25b86b 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
}
@ammario
Copy link
Author

ammario commented Aug 29, 2023

@wilrodriguez I didn't realize how popular this gist was and recklessly updated it. I reverted it back per your message. Thank you!

@barbiequeue
Copy link

Really helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment