Skip to content

Instantly share code, notes, and snippets.

@CerebralMastication
Created June 22, 2011 16:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CerebralMastication/1040479 to your computer and use it in GitHub Desktop.
Save CerebralMastication/1040479 to your computer and use it in GitHub Desktop.
convert IP address to integer
ip2Int <- function(ip){
split <- as.numeric(strsplit(ip, "\\.")[[1]])
out <- (split[1] * 256^3) + (split[2] * 256^2) + (split[3] * 256) + (split[4])
return(out)
}
int2Ip <- function(int){
split <- NULL
split[1] <- as.integer(int/256^3)
int <- int - split[1] * (256^3)
split[2] <- as.integer(int/256^2)
int <- int - split[2] * (256^2)
split[3] <- as.integer(int/256)
int <- int - split[3] * 256
split[4] <- int
return(paste(split, collapse = "."))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment