开发者

Converting IP from C string to unsigned int?

开发者 https://www.devze.com 2023-04-05 03:11 出处:网络
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开发者_StackOverflow sure there should be some function for that but I havn\

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开发者_StackOverflow sure there should be some function for that but I havn't found it. Any ideas?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消