Last active
April 14, 2016 08:22
-
-
Save philippreston/681f16fadb0385347a7e04ec32f89d6f to your computer and use it in GitHub Desktop.
One Line IP to Long and Back
This file contains hidden or 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
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