Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert integer value to ip-address

Tags:

c

embedded

How to convert a 32-bit integer value to an ip-address?

I am having int value=570534080 and want to convert it to 192.168.1.34.

like image 617
Shiva Avatar asked Sep 28 '13 09:09

Shiva


1 Answers

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(void) {
    int value=570534080;
    struct in_addr addr = {value};
    printf( "%s", inet_ntoa( addr ) );
    return 0;
}

Test: http://ideone.com/RCDgj4

For Windows use #include <winsock2.h>

like image 113
kotlomoy Avatar answered Nov 03 '22 21:11

kotlomoy