scala ipコンバータ

820 ワード

spark         ip            ,   ip       ,        。     scala ip              。

object ip {
def main(args: Array[String]) {
long2ip(ip2long("12222.168.1.1000"))
}

def ip2long(ip: String): Long ={
val Array(a, b, c, d) = ip.split( """\.""")
val retVal = (a.toLong << 24) + (b.toLong << 16) + (c.toLong << 8) + (d.toLong)
printf(s"value=$retVal
")
retVal
}

def long2ip(ip:Long): Unit ={

val a:Long = (ip >>> 24) & 0xff
val b:Long = (ip >>> 16) & 0xff
val c:Long = (ip >>> 8) & 0xff
val d:Long = (ip) & 0xff

printf(s"ip=$a.$b.$c.$d")
}
}