Skip to content

Instantly share code, notes, and snippets.

@mkearney
Created August 27, 2019 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkearney/20e4594b1ca424a0d20b5124388742b4 to your computer and use it in GitHub Desktop.
Save mkearney/20e4594b1ca424a0d20b5124388742b4 to your computer and use it in GitHub Desktop.
Convert bytes in string to appropriate char(s)
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