Skip to content

IP Address Integer Format

Some API fields use signed 32-bit integers for IPv4 addresses, for example:

  • ipAddress from GET /api/v1/servers
  • ipAddress from GET /api/v1/servers/{ip}/{port}
  • currentServerIp from GET /api/v1/players
  • server.ipAddress from GET /api/v1/players/{uuid}/sessions

Why signed? PostgreSQL integer is signed 32-bit, so addresses >= 128.0.0.0 appear as negative values.

  1. Convert the signed value to an unsigned 32-bit integer.
  2. Split into 4 octets using bit shifts and masks.
  3. Join octets with dots.
function intToIp(ipInt) {
const unsigned = ipInt >>> 0;
return [
(unsigned >>> 24) & 255,
(unsigned >>> 16) & 255,
(unsigned >>> 8) & 255,
unsigned & 255
].join('.');
}
console.log(intToIp(16909060)); // 1.2.3.4
console.log(intToIp(-939524096)); // 200.0.0.0