inetaton java_Java的inet_aton inet_ntoa

public static String inet_ntoa(long add) {

return ((add & 0xff000000) >> 24) + "." + ((add & 0xff0000) >> 16)

+ "." + ((add & 0xff00) >> 8) + "." + ((add & 0xff));

}

public static long inet_aton(Inet4Address add) {

byte[] bytes = add.getAddress();

long result = 0;

for (byte b : bytes) {

if ((b & 0x80L) != 0) {

result += 256L + b;

} else {

result += b;

}

result <<= 8;

}

result >>= 8;

return result;

}

/**

* significantly faster than parse the string into long

*/

public static long inet_aton(String add) {

long result = 0;

// number between a dot

long section = 0;

// which digit in a number

int times = 1;

// which section

int dots = 0;

for (int i = add.length() - 1; i >= 0; --i) {

if (add.charAt(i) == '.') {

times = 1;

section <<= dots * 8;

result += section;

section = 0;

++dots;

} else {

section += (add.charAt(i) - '0') * times;

times *= 10;

}

}

section <<= dots * 8;

result += section;

return result;

}没有找到好的java版,在大规模使用的时候还是有些差距的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值