Skip to content

Instantly share code, notes, and snippets.

@ChaseH88
Created September 13, 2021 09:10
Show Gist options
  • Select an option

  • Save ChaseH88/759fa4d2f27ea41cbfb1e3348be34e7d to your computer and use it in GitHub Desktop.

Select an option

Save ChaseH88/759fa4d2f27ea41cbfb1e3348be34e7d to your computer and use it in GitHub Desktop.
Converts a number to an IPv4 string
/**
* 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