Linux in_addr_t 到字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3768183/
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
in_addr_t to string
提问by cateof
I have an IP address stored in in_addr_t
and I want to create the corresponding string representation of this data type (e.g. in_addr_t
to 10.0.0.1
).
我有一个存储在其中的 IP 地址in_addr_t
,我想创建此数据类型的相应字符串表示形式(例如in_addr_t
to 10.0.0.1
)。
How can I do that?
我怎样才能做到这一点?
采纳答案by James Anderson
Use inet_ntop()
- convert IPv4 and IPv6 addresses from binary to text form.
使用inet_ntop()
- 将 IPv4 和 IPv6 地址从二进制格式转换为文本格式。
回答by bhups
char *inet_ntoa(struct in_addr in);
is the desired function.
char *inet_ntoa(struct in_addr in);
是所需的功能。
回答by Farhad Malekpour
-(NSString*)long2ip:(uint32_t)ip
{
char str[40];
struct in_addr myaddr;
myaddr.s_addr = htonl(ip);
if (inet_ntop(AF_INET, &myaddr, str, sizeof(str)))
{
return [NSString stringWithFormat:@"%s",str];
}
else
{
return nil;
}
}