windows 将 IP 从 C 字符串转换为无符号整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7443846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting IP from C string to unsigned int?
提问by KaiserJohaan
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?
我有一个简短的问题;我在 C 字符串中有一个 IPv4(比如“192.168.0.1”),我想将它转换为 uint32_t。我确定应该有一些功能,但我还没有找到。有任何想法吗?
回答by cnicutar
The function is called inet_aton
.
该函数被调用inet_aton
。
int inet_aton(const char *cp, struct in_addr *inp);
The structure in_addr is defined in <netinet/in.h>
as:
in_addr 结构体定义<netinet/in.h>
为:
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
.
当然你也可以使用较新的函数inet_pton
。