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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:14:47  来源:igfitidea点击:

in_addr_t to string

clinuxnetwork-programming

提问by cateof

I have an IP address stored in in_addr_tand I want to create the corresponding string representation of this data type (e.g. in_addr_tto 10.0.0.1).

我有一个存储在其中的 IP 地址in_addr_t,我想创建此数据类型的相应字符串表示形式(例如in_addr_tto 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;
    }
}