国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

ip和int互轉(zhuǎn)的scala版本

2019-11-11 01:36:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

ip和int互轉(zhuǎn)的scala版本

有個(gè)群友問(wèn)scala版本,剛好自己簡(jiǎn)單實(shí)現(xiàn)了個(gè),發(fā)一下代碼

代碼:

import java.net.InetAddress/** * @author todd.chen at 16/01/2017 10:50. * email : todd.chen@ximalaya.com */object NetworkUtil { /** * get local ip with Int * * @return ip int */ def getLocalIp: Int = { val ip = InetAddress.getLocalHost.getHostAddress ip2Int(ip) } /** * ip transform to int * * @param ipAddress ip string * @return ip int */ def ip2Int(ipAddress: String): Int = { ipAddress.split("http://.").zipWithIndex.foldLeft(0) { case (result, (ip, index)) ? result | ip.toInt << (index * 8) } } /** * int transform to ip * * @param ip int ip * @return string ip */ def int2Ip(ip: Int): String = { s"${(0 to 3).map(i ? (ip >> 8 * i) & 0xFF).mkString(".")}" }}

測(cè)試:

import org.scalatest.{FunSuite, Matchers}/** * @author todd.chen at 02/02/2017 11:23. * email : todd.chen@ximalaya.com */class NetworkUtilTest extends FunSuite with Matchers { test("testIp2Int") { val ip = NetworkUtil.ip2Int("192.168.120.60") ip should be(1014540480) } test("testInt2Ip") { val ip = 1014540480 val ipString = NetworkUtil.int2Ip(ip) ipString should be("192.168.120.60") } test("getLocalIp"){ val ip = NetworkUtil.getLocalIp val ipString = NetworkUtil.int2Ip(ip) val ipInt = NetworkUtil.ip2Int(ipString) ip should equal(ipInt) }}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 渭南市| 定州市| 湄潭县| 许昌县| 富源县| 平乐县| 安丘市| 南华县| 中阳县| 方正县| 民和| 渑池县| 应用必备| 房产| 新丰县| 黄石市| 灯塔市| 普陀区| 都江堰市| 河北省| 汝城县| 楚雄市| 铜陵市| 阿拉善右旗| 金寨县| 资中县| 古丈县| 额敏县| 南开区| 大石桥市| 德钦县| 玛沁县| 永定县| 大化| 吴桥县| 中阳县| 洛阳市| 武陟县| 乳山市| 印江| 磐安县|