将IP从C字符串转换为unsigned int? [英] Converting IP from C string to unsigned int?

查看:159
本文介绍了将IP从C字符串转换为unsigned int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个快速的问题;我在C字符串中有一个IPv4(比如192.168.0.1),我想将它转换为uint32_t。我敢肯定应该有一些功能,但我没有找到它。有什么想法?

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?

推荐答案

该函数名为 inet_aton

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

结构in_addr在< netinet / in.h> <中定义/ code> as:

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;
};

当然你也可以使用更新的功能 inet_pton

Of course you can also use the newer function inet_pton.

这篇关于将IP从C字符串转换为unsigned int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆