Sai A Sai A
Updated date Nov 29, 2023
This blog will explore how to convert an Integer to an IPv4 Address in C. From bitwise operations to union utilization.

Introduction:

IP addresses are the fundamental building blocks that enable communication between devices. While we commonly encounter IP addresses in the familiar dotted-decimal format (like 192.168.1.1), behind the scenes, computers interpret them as integers. In this blog, we will explore how to convert integers to their corresponding IPv4 addresses in the C programming language.

Method 1: Bitwise Shifting and Masking

This method uses bitwise shifting and masking to extract the four octets of the IPv4 address. Here's a breakdown:

  • (ipInteger >> 24) & 0xFF: Right-shift the integer 24 bits to get the first octet and mask it with 0xFF to extract the last 8 bits.
  • (ipInteger >> 16) & 0xFF: Right-shift the integer 16 bits to get the second octet.
  • (ipInteger >> 8) & 0xFF: Right-shift the integer 8 bits to get the third octet.
  • ipInteger & 0xFF: Mask the integer to get the fourth octet.
#include <stdio.h>

// Function to convert integer to IPv4 address
void intToIp(unsigned int ipInteger, char ipString[]) {
    sprintf(ipString, "%d.%d.%d.%d", (ipInteger >> 24) & 0xFF, (ipInteger >> 16) & 0xFF, (ipInteger >> 8) & 0xFF, ipInteger & 0xFF);
}

int main() {
    // Example: Convert integer 3232235521 to IPv4 address
    unsigned int ipAddress = 3232235521;
    char ipString[16]; // IPv4 addresses are at most 15 characters long

    // Call the conversion function
    intToIp(ipAddress, ipString);

    // Display the result
    printf("Method 1 - Bitwise Shifting and Masking:\n");
    printf("Integer: %u\nIPv4 Address: %s\n", ipAddress, ipString);

    return 0;
}

Output:

Method 1 - Bitwise Shifting and Masking:
Integer: 3232235521
IPv4 Address: 192.168.1.1

The program converts the integer 3232235521 to its corresponding IPv4 address using bitwise shifting and masking, resulting in the output IPv4 address 192.168.1.1.

Method 2: Utilizing Union

An alternative approach involves using a union to interpret the same memory location as both an integer and an array of four bytes. This method provides a fresh perspective on how computers store and represent data in memory.

#include <stdio.h>

union IpConverter {
    unsigned int ipAddress;
    unsigned char ipBytes[4];
};

int main() {
    // Example: Convert integer 3232235521 to IPv4 address using union
    union IpConverter converter;
    converter.ipAddress = 3232235521;

    // Display the result
    printf("Method 2 - Utilizing Union:\n");
    printf("Integer: %u\nIPv4 Address: %u.%u.%u.%u\n", converter.ipAddress, converter.ipBytes[0], converter.ipBytes[1], converter.ipBytes[2], converter.ipBytes[3]);

    return 0;
}

Output:

Method 2 - Utilizing Union:
Integer: 3232235521
IPv4 Address: 192.168.1.1

The program uses a union (IpConverter) to interpret the same memory location as both an integer and an array of four bytes, providing an alternative method for converting the integer 3232235521 to its corresponding IPv4 address.

Conclusion:

This blog has explored converting an integer to an IPv4 Address in C. The bitwise shifting and masking method provides a granular, low-level perspective, showcasing how computers extract specific bits to construct an IPv4 address. Conversely, the union method presents an alternative viewpoint, emphasizing the flexibility and interpretability of data in memory.

Comments (0)

There are no comments. Be the first to comment!!!