如何将R中的转义十六进制数转换为以html格式格式化的十六进制数

问题描述 投票:0回答:1

例如:R的表情符号转义为“ \ U0001f923”,我想将其传递到html文件,该文件的格式可用作以下字符:&#x 1 f 9 2 3; (🤣)。这是我第一次在stackoverflow中发帖,显示了我对这个问题的担忧。大家都保持安全。

html r hex
1个回答
0
投票

您可以组合使用功能utf8ToIntas.hexmode,然后将转义字符粘贴到:

as.html <- function(x) paste0("&x", as.hexmode(utf8ToInt(x)), ";")
as.html("\U0001f923")
#> [1] "&x1f923;"

您可以使用它来替换多字节字符,如下所示:

replace_multibytes <- function(x)
{
  as.html <- function(x) paste0("&x", as.hexmode(utf8ToInt(x)), ";")
  char_list <- as.list(unlist(strsplit(x, "")))
  x <- sapply(char_list, function(y) if(length(charToRaw(y)) > 1) as.html(y) else y)
  paste0(x, collapse = "")
}

现在您可以这样做

replace_multibytes("The following need replaced: \U0001f923 \U0001f924")
#> [1] "The following need replaced: &x1f923; &x1f924;"
© www.soinside.com 2019 - 2024. All rights reserved.