How To Convert Ip Address To Decimal In C?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use the inet_pton() function to convert an IP address to its decimal equivalent in C. The prototype for this function is as follows:
int inet_pton (int af, const char *src, void * dst);
Where:
•af – specifies the address family (AF_INET for IPv4 addresses, AF_INET6 for IPv6 addresses).
•src – A pointer to the string IP address.
•dst – A pointer to a buffer to store the resulting binary data. //
Example code
#include <arpa/inet.h>
#include <stdio.h> int main(void) { char ipaddr[] = “192.168.1.1“;
int ret; uint32_t network_order; ret = inet_pton(AF_INET,
ipaddr, &network_order);
if (ret == 1) { printf(“IPv4 Address: %u\n“, ntohl(network_order));
}
return 0;
}