mirror of
https://github.com/meshtastic/web.git
synced 2026-06-12 07:25:03 -04:00
Added ip utils
This commit is contained in:
13
src/core/utils/ip.ts
Normal file
13
src/core/utils/ip.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
export function convertIntToIpAddress(int: number): string {
|
||||
return `${int & 0xff}.${(int >> 8) & 0xff}.${(int >> 16) & 0xff}.${(int >> 24) & 0xff}`;
|
||||
}
|
||||
|
||||
export function convertIpAddressToInt(ip: string): number | null {
|
||||
const parts = ip.split('.').map(Number).reverse(); // little-endian byte order
|
||||
|
||||
if (parts.some(Number.isNaN)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return parts.reduce((total, part) => (total << 8) | part, 0);
|
||||
}
|
||||
Reference in New Issue
Block a user