Created
August 27, 2019 20:34
-
-
Save mkearney/20e4594b1ca424a0d20b5124388742b4 to your computer and use it in GitHub Desktop.
Convert bytes in string to appropriate char(s)
This file contains hidden or 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
bytes2char <- function(x) { | |
m <- gregexpr("<[[:alnum:]]{2}>", x) | |
y <- regmatches(x, m)[[1]] | |
y <- gsub("^<|>$", "", y) | |
y <- strsplit(y, "[><]+") | |
regmatches(x, m) <- list(sapply(y, function(.x) rawToChar(as.raw(paste0("0x", .x))))) | |
x | |
} | |
## read in string with bytes | |
x <- iconv("“This <cat>’s tail is curly.”'", to = "ascii", sub = "byte") | |
## before | |
x | |
#> [1] "<e2><80><9c>This <cat><e2><80><99>s tail is curly.<e2><80><9d>'" | |
## after | |
bytes2char(x) | |
#> [1] "“This <cat>’s tail is curly.”'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment