in_addr_t to string

Viewed 23836

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).

How can I do that?

3 Answers
-(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;
    }
}
Related