Last active
December 15, 2015 03:09
-
-
Save tomgullo/5192677 to your computer and use it in GitHub Desktop.
ip to decimal 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
def toStringIp(long l) { | |
return ( (l >> 24) & 0xFF) + "." + ( (l >> 16) & 0xFF) + "." + | |
( (l >> 8) & 0xff) + "." + (l & 0xFF); | |
} | |
def long toLong(String ip) { | |
if (!ip) { | |
return -1 | |
} | |
def octets = ip.split("\\.") | |
long ip_ret = 0 | |
if (octets.size() != 4) { | |
return -1 | |
} | |
for (int i = 3; i >= 0; i--) { | |
long octet = Long.parseLong(octets[3 - i]); | |
if (octet > 255 || octet < 0) { | |
return -1 | |
} | |
ip_ret |= octet << (i * 8) | |
} | |
return ip_ret | |
} | |
rand = new Random() | |
max = 255 | |
def success_list = [] | |
def getRandom() { | |
return rand.nextInt(max + 1) | |
} | |
def s = (getRandom() as String) + "." + (getRandom() as String) + "." + (getRandom() as String) + "." + (getRandom() as String) | |
def to_l = toLong(s.trim()) | |
def back = toStringIp(to_l) | |
if (s != back) { | |
println "not equal" | |
} else { | |
println "OK" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment