I've got a quick question; I have an IPv4 in a C string (say "192.168.0.1") and I want to convert it to an uint32_t. I'm sure there should be some function for that but I havn't found it. Any ideas?
Asked
Active
Viewed 7,363 times
4
-
`uint32_t` is not an appropriate type for addresses. This is 2011 and modern software **must** support IPv6. Store addresses as strings for any data interchange, and in the `struct addrinfo *` returned by `getaddrinfo` for internal user. – R.. GitHub STOP HELPING ICE Sep 16 '11 at 13:13
1 Answers
10
The function is called inet_aton
.
int inet_aton(const char *cp, struct in_addr *inp);
The structure in_addr 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 function inet_pton
.

cnicutar
- 178,505
- 25
- 365
- 392
-
-
2@KaiserJohaan Yes, and inside it you will find `s_addr` which is `unit32_t` – cnicutar Sep 16 '11 at 11:16
-
When it comes to windows i guess you are stuck with inet_addr? Since inet_aton dosnt seem to be defined in windows – KaiserJohaan Sep 16 '11 at 11:25
-
1
-
inet_pton is only supported after windows vista. http://msdn.microsoft.com/en-us/library/windows/desktop/cc805844(v=vs.85).aspx As far as I know for xp you have to use inet_aton. – rem7 Jan 14 '12 at 17:38
-
-