한 가지 방법이 있습니다. 지정하는 ipv4 주소는 기본적으로 8 바이트 16 진수의 십진수 표현이므로 아래와 같이 변환하여 십진 정수로 표시 할 수 있습니다.
a.b.c.d = a × 2563 + b × 2562 + c × 256 + d
그래서 우리는 테스트 벡터 (이 예에서는 ip
), 그리고 (이 예에서는 ip.range
) 범위의 데이터 프레임하고있는 간단한 연산을 사용하여 IP 이동하는 범위 식별 모두에 대해이 작업을 수행합니다.
# example dataset
ip <- c("178.91.21.2","178.91.19.30","178.91.20.100")
ip.range <- data.frame(start=c("178.91.19.0", "178.91.20.0", "178.91.21.0"),
end= c("178.91.19.255","178.91.20.255","178.91.21.255"),
stringsAsFactors=FALSE)
# function to convert ip address to decimal integer
ip2integer <- function(ip) sapply(strsplit(ip,".",fixed=TRUE),function(x)sum(as.integer(x)*256^(3:0)))
# convert ip and ranges to integer
ip.int <- ip2integer(ip)
range.int <- data.frame(sapply(ip.range,ip2integer))
# find indices, combine into result
indx <- sapply(ip.int,function(x)with(range.int,which(x>=start & x <=end)))
result <- cbind(ip,ip.range[indx,])
result
# ip start end
# 3 178.91.21.2 178.91.21.0 178.91.21.255
# 1 178.91.19.30 178.91.19.0 178.91.19.255
# 2 178.91.20.100 178.91.20.0 178.91.20.255
나는 C++있어 - 순수 R 버전에 비해 정수로 훨씬 빠르게 IPv4의 변환과 백업 [iptools] (https://gitlab.dds.ec/public/projects/bob.rudis/iptools) 패키지 . 그러나 IP 변환이 필요하고 다른 비트가 필요하지 않은 경우 독립 실행 형 버전 [http://datadrivensecurity.info/blog/posts/2014/May/vectorizing-ipv4-address-conversions-part-2/]이 있습니다. – hrbrmstr