
	                	                 
ip2long 함수는 주소값을 연관된 IP 주소로 변환해주는 함수입니다. 
ip2long 함수의 사용법은 다음과 같습니다.
#hostingforum.kr
c
#include 
long ip2long(const char *ip) {
    struct in_addr addr;
    inet_pton(AF_INET, ip, &addr);
    return ntohl(addr.s_addr);
}
이 함수는 주어진 IP 주소를 32비트 정수형으로 변환합니다.
예를 들어, 192.168.0.1이라는 IP 주소를 0x000000C0A80001L 형태로 변환하는 방법은 다음과 같습니다.
#hostingforum.kr
c
#include 
int main() {
    const char *ip = "192.168.0.1";
    long ipLong = ip2long(ip);
    printf("%lxn", ipLong); // 0xc0a80001 출력
    return 0;
}
ip2long 함수가 반환하는 값을 사용하는 방법은 다음과 같습니다.
#hostingforum.kr
c
#include 
int main() {
    const char *ip = "192.168.0.1";
    long ipLong = ip2long(ip);
    struct in_addr addr;
    addr.s_addr = ipLong;
    char ipStr[INET_ADDRSTRLEN];
    inet_ntop(AF_INET, &addr, ipStr, INET_ADDRSTRLEN);
    printf("%sn", ipStr); // 192.168.0.1 출력
    return 0;
}
이 함수는 주어진 IP 주소를 문자열 형태로 변환합니다.
2025-05-27 11:39