* 查詢接口javascript
{"success":"1","result":{"status":"OK","ip":"47.96.133.100","ip_str":"47.96.133.1","ip_end":"47.96.133.254","inet_ip":"794854756","inet_str":"794854657","inet_end":"794854910","areano":"0571","postno":"310000","operators":"阿里雲","att":"中國,浙江,杭州","detailed":"中國浙江杭州 阿里雲/電信/聯通/移動/鐵通/教育網","area_style_simcall":"中國,浙江,杭州","area_style_areanm":"中華人民共和國,浙江省,杭州市"}}
* IP地址轉化爲整數 (js實現)html
function ip2int(ip) { var i = ip.split('.').map(function(s) { return s.split('').reduce(function(a, c, i) { return (a = 10*a + c.charCodeAt(0)-48) }, 0); }).reduce(function(a, c, i, array) { a |= c << (array.length-i-1)*8; return a; }, 0); if (i < 0) { i += 0x0000000100000000; } return i; }
var ip_int = ip2int("47.96.133.100"); console.log(ip_int);
Output:java
794854756json
console.log( ip2int("172.16.0.224") );ubuntu
2886729952api
經過http://www.bejson.com/convert/ip2int/驗證app
參考ECMA-262ide
12.8.3.1 Runtime Semantics: Evaluation ShiftExpression : ShiftExpression << AdditiveExpression 1. Let lref be the result of evaluating ShiftExpression. 2. Let lval be GetValue(lref). 3. ReturnIfAbrupt(lval). 4. Let rref be the result of evaluating AdditiveExpression. 5. Let rval be GetValue(rref). 6. ReturnIfAbrupt(rval). 7. Let lnum be ToInt32(lval). 8. ReturnIfAbrupt(lnum). 9. Let rnum be ToUint32(rval). 10. ReturnIfAbrupt(rnum). 11. Let shiftCount be the result of masking out all but the least significant 5 bits of rnum, that is, compute rnum & 0x1F. 12. Return the result of left shifting lnum by shiftCount bits. The result is a signed 32-bit integer.
172.16.0.1 會變爲負數函數
根據ECMA-262文檔,移位操做返回值 是 signed int 32位, 因此無法經過 &= 0x00000000ffffffff 把32bit最高位置爲0, - 轉換爲正數 - 只能加上4294967296
* IP 整數轉換爲字符串
function int2ip(bytes) { var a = bytes >> 24, b = bytes >> 16 & 0x00ff, c = bytes >> 8 & 0x000000ff, d = bytes & 0x000000ff; return a +'.' + b + '.' +c + '.' +d; }
test case: 3083241027
"-73.198.134.67"
最前面一個字節出現了負值 ,由於最高位爲1,因此須要 & 0x00ff,把高位置爲0
function int2ip(bytes) { var a = (bytes >> 24)&0x00ff, b = bytes >> 16 & 0x00ff, c = bytes >> 8 & 0x000000ff, d = bytes & 0x000000ff; return a +'.' + b + '.' +c + '.' +d; }
php 實現:
* long2ip.php
<?php function mylong2ip($long) { $long &= 0xffffffff; $a = [ ($long >> 24)&0x00ff, ($long >> 16)&0x00ff, ($long >> 8)&0x0000ff, $long & 0xff ]; return implode('.', $a); } function myip2long($ip) { $arr = explode('.', $ip); $s2i = function($s) { $n = strlen($s); $acc = 0; for ($i = 0; $i < $n; $i++) { $acc = 10 * $acc + (ord($s[$i]) - 48); } return $acc; }; list($a, $b, $c, $d) = array_map($s2i, $arr); return ($a<<24) | ($b << 16) | ($c <<8) | $d; }
// test:
$ip = mylong2ip(794854756); // 47.96.133.100
echo $ip.PHP_EOL;
$long = myip2long($ip);
echo $long.PHP_EOL;
// run:
ubuntu@et-dev-mingzhanghui:~/code/php$ php long2ip.php
47.96.133.100
794854756
ip地址換化爲整數爲負數? + 4294967296
2 * (2147483647+1)
相關的php函數:
http://php.net/manual/en/function.list.php
http://php.net/manual/en/function.array-map.php
http://php.net/manual/en/function.ord.php