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
-(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;
}
}
Use inet_ntop() - convert IPv4 and IPv6 addresses from binary to text form.
char *inet_ntoa(struct in_addr in);
is the desired function.