Last active
August 29, 2015 14:16
-
-
Save bhaskarvk/eb4587f27157129d9c78 to your computer and use it in GitHub Desktop.
Vectorized functions to convert ipv4 to/from integer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Vectorized functions to convert ipv4 to/from integer | |
require(bitops) # required for unsigned int bitwise ops | |
int_to_ipv4 <- function(i) { | |
return(lapply(i,function(x) { | |
paste(bitAnd(bitShiftR(x,c(24,16,8,0)),0xFF),collapse='.') | |
})) | |
} | |
ipv4_to_int <- function(i) { | |
return(lapply(i,function(x) { | |
sum(bitShiftL(as.integer(strsplit(x,'.',fixed = T)[[1]]),c(24,16,8,0))) | |
})) | |
} | |
# Parallel versions | |
require(parallel) | |
par_int_to_ipv4 <- function(i) { | |
return(mclapply(i,function(x) { | |
paste(bitAnd(bitShiftR(x,c(24,16,8,0)),0xFF),collapse='.') | |
})) | |
} | |
par_ipv4_to_int <- function(i) { | |
return(mclapply(i,function(x) { | |
sum(bitShiftL(as.integer(strsplit(x,'.',fixed = T)[[1]]),c(24,16,8,0))) | |
})) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment