Skip to content

Instantly share code, notes, and snippets.

@OElesin
Created July 4, 2016 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OElesin/f0f2c69530a315177b9e0227a140f9c1 to your computer and use it in GitHub Desktop.
Save OElesin/f0f2c69530a315177b9e0227a140f9c1 to your computer and use it in GitHub Desktop.
This service class converts IP to long and reverse. This class is useful for generating userIDs from IP addresses if user IDs are not present in your data.
def ipToLong(ipAddress: String): Long = {
ipAddress.split("\\.").reverse.zipWithIndex.map(a=>a._1.toInt*math.pow(256,a._2).toLong).sum
}
def longToIP(long: Long): String = {
(0 until 4).map(a=>long / math.pow(256, a).floor.toInt % 256).reverse.mkString(".")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment