Skip to content

Instantly share code, notes, and snippets.

@viko16
Created September 8, 2016 05:54
Show Gist options
  • Save viko16/6e036debb879e15b67fd41b2dc081190 to your computer and use it in GitHub Desktop.
Save viko16/6e036debb879e15b67fd41b2dc081190 to your computer and use it in GitHub Desktop.
IP 地址的多种玩法
var ipStr = '180.97.33.107'
var arr = ipStr.split('.').map((el => parseInt(el, 10)))
// 十进制
var ten = arr[0] * Math.pow(2, 24) + arr[1] * Math.pow(2, 16) + arr[2] * Math.pow(2, 8) + arr[3]
console.log('http://' + ten)
// 十六进制
var sixteen = ten.toString(16)
console.log('http://0x' + sixteen)
// 八进制
var eight = ten.toString(8)
console.log('http://0' + eight)
// 十六进制分段
var sixteenPart = arr.map(el => '0x' + el.toString(16))
console.log('http://' + sixteenPart.join('.'))
// 八进制分段
var eightPart = arr.map(el => '0' + el.toString(8))
console.log('http://' + eightPart.join('.'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment