Created
September 13, 2021 09:10
-
-
Save ChaseH88/759fa4d2f27ea41cbfb1e3348be34e7d to your computer and use it in GitHub Desktop.
Converts a number to an IPv4 string
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
| /** | |
| * Converts a number into an IPv4 address. | |
| * @param int Number to be converted | |
| * @returns {string} - formatted value | |
| * @example let ipAddress = formatIntToIP(167772160) | |
| */ | |
| const formatIntToIP = (int: number): string => ( | |
| `${(int>>>24)}.${(int>>16 & 255)}.${(int>>8 & 255)}.${(int & 255)}` | |
| ); | |
| const intsToConvert: number[] = [ | |
| 167772160, | |
| 169645570, | |
| 169645575, | |
| 172722688 | |
| ]; | |
| for (let int of intsToConvert){ | |
| console.log(formatIntToIP(int)) | |
| } | |
| // Outputs: | |
| // 10.0.0.0 | |
| // 10.28.150.2 | |
| // 10.28.150.7 | |
| // 10.75.138.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment