题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
http://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
while(line = readline()) {
if(line.includes(".")) {
toNum(line)
} else {
toIp(line)
}
}
function toIp(num) {
const str = Number(num).toString(2).padStart(32, "0")
const arr = [str.slice(0,8), str.slice(8,16), str.slice(16,24), str.slice(24,32)]
const ip = arr.map(ele => parseInt(ele, 2)).join(".")
print(ip)
}
function toNum(ip) {
const arr = ip.split(".").map(ele => Number(ele).toString(2)).map(ele => ele.padStart(8, "0"))
const num = arr.join("")
print(parseInt(num, 2))
}
// const a = "10"
// const b = "10".padStart(8, "0")
// print(b)