Q:

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

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Number Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program to Print Natural Numbers in Reverse orde... >>
<< C Program to Evaluate Exponential Series using Fun...