Skip to content
Related Articles

Related Articles

Program to convert IP address to hexadecimal

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 02 Dec, 2021
Improve Article
Save Article

Given an IP Address and task is to change the IP address equivalent the hexadecimal value.

Input  :
Output : 0x7f000001

Input  :
Output : 0xac1f0002

using the Library function to convert the IP address to convert into the hexadecimal value we use ” arpa/inet.h “ header file. The inet_addr() function shall convert the string in the standard IPv4 dotted decimal notation, to an integer value suitable for use as an Internet address.


// C++ program for IP to
// hexadecimal conversion
#include <arpa/inet.h>
#include <iostream>
#include <string.h>
using namespace std;
// function for reverse hexadecimal number
void reverse(char* str)
    // l for swap with index 2
    int l = 2;
    int r = strlen(str) - 2;
    // swap with in two-2 pair
    while (l < r) {
        swap(str[l++], str[r++]);
        swap(str[l++], str[r]);
        r = r - 3;
// function to conversion and print
// the hexadecimal value
void ipToHexa(int addr)
    char str[15];
    // convert integer to string for reverse
    sprintf(str, "0x%08x", addr);
    // reverse for get actual hexadecimal
    // number without reverse it will
    // print 0x0100007f for
    // print string
    cout << str << "\n";
// Driver code
int main()
    // The inet_addr() function  convert  string
    // in to standard IPv4 dotted decimal notation
    int addr = inet_addr("");
    return 0;



My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!