Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,872 questions

51,796 answers

573 users

How to convert int to IP address in C

4 Answers

0 votes
#include <stdio.h>
#include <arpa/inet.h>

int main()
{
    uint32_t ip = 2912054386;
    
    struct in_addr ip_address;
    ip_address.s_addr = ip;
    
    printf("The IP address is %s\n", inet_ntoa(ip_address));
    
    return 0;
}



/*
run:

The IP address is 114.108.146.173

*/

 



answered May 15, 2024 by avibootz
0 votes
#include <stdio.h>

void get_ip_address(unsigned char bytes[], unsigned int ip) {
    bytes[0] = ip & 0xFF;
    bytes[1] = (ip >> 8) & 0xFF;
    bytes[2] = (ip >> 16) & 0xFF;
    bytes[3] = (ip >> 24) & 0xFF;   
}

int main()
{
    unsigned int ip = 2912054386;
    unsigned char bytes[4];
    
    get_ip_address(bytes, ip);
    
    printf("The IP address is %d.%d.%d.%d\n", bytes[0], bytes[1], bytes[2], bytes[3]);

    return 0;
}



/*
run:

The IP address is 114.108.146.173

*/

 



answered May 15, 2024 by avibootz
0 votes
#include <stdio.h>
 
typedef union IP {
    unsigned int ip;
    struct {
        unsigned char a;
        unsigned char b;
        unsigned char c;
        unsigned char d;
    } ips;
} IP;
 
int main()
{
    IP ip;
    ip.ip = 2912054386;
 
    printf("The IP address is %d.%d.%d.%d\n", ip.ips.a, ip.ips.b, ip.ips.c, ip.ips.d);
 
    return 0;
}
 
 
 
/*
run:
 
The IP address is 114.108.146.173
 
*/

 



answered May 15, 2024 by avibootz
edited May 15, 2024 by avibootz
0 votes
#include <stdio.h>

int main()
{
    unsigned int ip = 2912054386;

    printf("The IP address is %d.%d.%d.%d",
          ip & 0xFF,
          (ip >> 8) & 0xFF,
          (ip >> 16) & 0xFF,
          (ip >> 24) & 0xFF);

    return 0;
}



/*
run:

The IP address is 114.108.146.173

*/

 



answered May 15, 2024 by avibootz

Related questions

1 answer 173 views
173 views asked May 6, 2021 by avibootz
1 answer 213 views
1 answer 234 views
2 answers 232 views
232 views asked May 22, 2015 by avibootz
1 answer 184 views
...