iOS NSString转IP地址

 NSString *ipStr = @"192.168.2.1";
    
    struct in_addr addr;
    if (inet_aton([ipStr UTF8String], &addr) != 0)
    {
        uint32_t ip = ntohl(addr.s_addr);
        NSLog(@"%08x", ip);
    } else {
        NSLog(@"invalid address");
    }
    
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;
    zeroAddress.sin_addr = addr;

你可能感兴趣的:(iOS NSString转IP地址)