Need to convert IP addresses to integers for database storage or range comparisons? This guide covers everything about IPv4 to integer conversion via API, including the math behind it and implementation examples.
Why Convert IP to Integer?
IPv4 addresses are typically written in dotted-decimal notation (e.g., 192.168.1.1), but they're fundamentally 32-bit unsigned integers. Converting to integer form offers several advantages:
- Efficient database storage and indexing
- Fast range queries (e.g., "is IP in this range?")
- Simpler comparison operations
- Network calculations (subnetting, broadcasting)
The Conversion Formula
An IPv4 address consists of four octets, each a number from 0 to 255:
Integer = (octet1 × 256³) + (octet2 × 256²) + (octet3 × 256) + octet4
Example: 192.168.1.1
= (192 × 16777216) + (168 × 65536) + (1 × 256) + 1
= 3232235777
Using the IPv4 to Integer API
TinyFn provides a simple endpoint for IP to integer conversion:
GET https://api.tinyfn.io/v1/ip/to-integer?ip=192.168.1.1
Headers: X-API-Key: your-api-key
{
"ip": "192.168.1.1",
"integer": 3232235777,
"hex": "0xC0A80101",
"binary": "11000000101010000000000100000001"
}
Parameters
| Parameter | Type | Description |
|---|---|---|
ip |
string | IPv4 address in dotted-decimal notation (required) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/ip/to-integer?ip=192.168.1.1',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const { integer } = await response.json();
console.log(integer); // 3232235777
Python
import requests
response = requests.get(
'https://api.tinyfn.io/v1/ip/to-integer',
headers={'X-API-Key': 'your-api-key'},
params={'ip': '192.168.1.1'}
)
integer = response.json()['integer']
print(integer) # 3232235777
cURL
curl "https://api.tinyfn.io/v1/ip/to-integer?ip=192.168.1.1" \
-H "X-API-Key: your-api-key"
Common Use Cases
- Database Storage: Store IPs as integers for efficient indexing
- IP Range Checks: Easily check if an IP falls within a range
- Geolocation Databases: Fast IP-to-location lookups using integer ranges
- Access Control: Efficient IP whitelist/blacklist checking
- Network Tools: Calculate network addresses and broadcast addresses
Best Practices
- Use unsigned integers: Store as UNSIGNED INT in databases
- Validate input: Ensure IP addresses are valid before conversion
- Consider IPv6: For IPv6, you'll need 128-bit integers
- Index appropriately: Create database indexes for efficient range queries
Use via MCP
Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:
{
"mcpServers": {
"tinyfn-ip": {
"url": "https://api.tinyfn.io/mcp/ip/",
"headers": {
"X-API-Key": "your-api-key"
}
}
}
}
See all ip address tools available via MCP in our IP Address MCP Tools for AI Agents guide.
Try the IPv4 to Integer API
Get your free API key and start converting IP addresses in seconds.
Get Free API Key