Skip to content

Instantly share code, notes, and snippets.

@philippreston
Last active April 14, 2016 08:22
Show Gist options
  • Save philippreston/681f16fadb0385347a7e04ec32f89d6f to your computer and use it in GitHub Desktop.
Save philippreston/681f16fadb0385347a7e04ec32f89d6f to your computer and use it in GitHub Desktop.
One Line IP to Long and Back
ip = "192.168.0.1"
// Encode
e = (ip.split(/\./) as List).withIndex().collect { v,i->Long.parseLong(v) << (24 - (8 * i))}.inject(0) {a,b -> a+b }
// Decode
d = (0..3).collect{ (e >> (24 - (it * 8))) & 0xFF }.join(".")
assert d == ip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment