Skip to content

Instantly share code, notes, and snippets.

@ammario
Last active October 29, 2024 13:11
Show Gist options
  • 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!

@donuts-are-good
Copy link

The URL to this gist was produced by Phind-70B - thought you might find that interesting.

image

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