function ipTransform(ip) {
const arr = []
const binNums = ip.split(".").map((x) => parseInt(x).toString(2))
binNums.forEach(function(x) {
if(x.length < 8) {
const other = "0".repeat(8 - x.length) + x
}
})
}
ipTransform("128.32.10.1")
如何让我的变量成为一个数组呢?现在只有3个字符串
答案 0 :(得分:0)
我刚刚将代码编译为ES5:
"use strict";
function ipTransform(ip) {
var arr = [];
var binNums = ip.split(".").map(function (x) {
return parseInt(x).toString(2);
});
binNums.forEach(function (x) {
if (x.length < 8) {
var other = "0".repeat(8 - x.length) + x;
}
});
return binNums;
}
并得到了正确的结果。
> ipTransform('127.0.0.1')
> ["1111111", "0", "0", "1"]
答案 1 :(得分:0)
这应该有效:
AVG
&#13;
可替换地:
SELECT S.Name, AVG(ProductCount)
FROM Stores S
INNER JOIN (SELECT COUNT(ProductID) as ProductCount, StoreID
FROM Stores S
GROUP BY StoreID) P on S.ID = P.StoreID
GROUP BY S.Name
&#13;
这个有效,因为IP地址基本上都是256.所以你只需要对每个八位字节做一些数学运算并添加它们。
function ip_to_int32(ip)
{
// split the IP
// convert each part to binary
// make sure its 8 bits long
const binNums = ip.split(".").map((x) => (("00000000" + parseInt(x).toString(2))).slice(-8));
// join the array and then conver to integer using parseInt
return parseInt(binNums.join(""), 2);
}
console.log(ip_to_int32("128.32.10.1"));
console.log(ip_to_int32("10.10.10.1"));
console.log(ip_to_int32("192.168.1.1"));