Created
April 29, 2023 18:10
-
-
Save signalwerk/bef657f322ad57ed8ed84cbc848ec6e0 to your computer and use it in GitHub Desktop.
Alternative IP notations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// alternative ip notations | |
// https://ma.ttias.be/theres-more-than-one-way-to-write-an-ip-address/ | |
// https://twitter.com/h43z/status/1618220318023364608 | |
dotNotationToDec({ ip: "185.15.230.26" }); // liip.ch; | |
dotNotationToDec({ prefix: "185.", ip: "15.230.26" }); | |
/* | |
output: | |
dec http://3104826906/ | |
hex http://0xb90fe61a/ | |
oct http://0027103763032/ | |
185.dec http://185.1041946/ | |
185.hex http://185.0xfe61a/ | |
185.oct http://185.003763032/ | |
*/ | |
function dotNotationToDec({ ip, prefix = "" }) { | |
const decimalParts = ip.split(".").map((part) => parseInt(part, 10)); | |
const binaryParts = decimalParts.map((part) => | |
`00000000${part.toString(2)}`.slice(-8) | |
); | |
const binary = binaryParts.join(""); | |
const decimal = parseInt(binary, 2); | |
print(decimal, prefix); | |
} | |
function print(decimal, prefix = "") { | |
const hex = decimal.toString(16); | |
const oct = decimal.toString(8); | |
// binary to decimal | |
console.log(`${prefix}dec http://${prefix}${decimal}/`); | |
console.log(`${prefix}hex http://${prefix}0x${hex}/`); | |
console.log(`${prefix}oct http://${prefix}00${oct}/`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment