Converting IP from string C to unsigned int?

I have a quick question; I have IPv4 in line C (say, "192.168.0.1") and I want to convert it to uint32_t. I am sure there must be some function for this, but I have not found it. Any ideas?

+4
source share
1 answer

The function is called inet_aton .

 int inet_aton(const char *cp, struct in_addr *inp); 

The in_addr structure is defined in <netinet/in.h> as:

 typedef uint32_t in_addr_t; struct in_addr { in_addr_t s_addr; }; 

Of course, you can also use the newer inet_pton function.

+10
source

All Articles