C Program to convert Binary IP address to 32-bit long int

By | 05.11.2016

Binary IP address to 32-bit long int


Write a C Program to convert Binary IP address to 32-bit long int. Here’s simple C Program to convert Binary IP address to 32-bit long int in C Programming Language.


Numbers in C


Normally, when we work with Numbers, we use primitive data types such as int, short, long, float and double, etc. The number data types, their possible values and number ranges have been explained while discussing C Data Types.


Here is source code of the C Program to convert Binary IP address to 32-bit long int. The C program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.


SOURCE CODE : :


/* C Program to convert Binary IP address to 32-bit long int  */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

union iptolint
{
    char ip[16];
    long  n;
};

long  conv(char []);

int main()
{
    union iptolint ipl;
    printf("Enter any IP Address to be converted :: ");
    scanf("%s",ipl.ip);

    ipl.n=conv(ipl.ip);

    printf("\nEquivalent 32-bit long int is :: %lu \n",ipl.n);
}

long conv(char ipadr[])
{
    long num=0,val;
    int p=24;
    char *tok,*ptr;
    tok=strtok(ipadr,".");
    while( tok != NULL)
    {
        val=strtol(tok,&ptr,10);
        num+=  val * (long)pow(2,p);
        p=p-8;
        tok=strtok(NULL,".");
    }
    return(num);
}

OUTPUT : :


/*  C Program to convert Binary IP address to 32-bit long int  */

Enter any IP Address to be converted :: 198.168.137.12

Equivalent 32-bit long int is :: 3332933900

Process returned 0

Above is the source code for C Program to convert Binary IP address to 32-bit long int which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

4 1 vote
Article Rating
Category: C Programming Number Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments