This function tries to detect character encoding.

detect_str_enc(x)

Arguments

x

Character vector.

Value

A character vector of length equal to the length of x and contains guessed iconv-compatible encodings names.

Examples

# detect character vector with ASCII strings ascii <- "I can eat glass and it doesn't hurt me." detect_str_enc(ascii)
#> [1] "ASCII"
# detect character vector with UTF-8 strings utf8 <- "\u4e0b\u5348\u597d" print(utf8)
#> [1] "下午好"
detect_str_enc(utf8)
#> [1] "UTF-8"
# function to read ASCII or UTF-8 files read_file <- function(x) readChar(x, file.size(x)) # path to examples ex_path <- system.file("examples", package = "uchardet") # russian text ru_utf8 <- read_file(file.path(ex_path, "ru.txt")) print(ru_utf8)
#> [1] "Я могу есть стекло, оно мне не вредит.\n"
detect_str_enc(iconv(ru_utf8, "utf8", "ibm866"))
#> [1] "IBM866"
detect_str_enc(iconv(ru_utf8, "utf8", "koi8-r"))
#> [1] "KOI8-R"
detect_str_enc(iconv(ru_utf8, "utf8", "cp1251"))
#> [1] "WINDOWS-1251"
# china text zh_utf8 <- read_file(file.path(ex_path, "zh.txt")) print(zh_utf8)
#> [1] "我能吞下玻璃而不傷身體。\n"
detect_str_enc(iconv(zh_utf8, "utf8", "big5"))
#> [1] "BIG5"
detect_str_enc(iconv(zh_utf8, "utf8", "gb18030"))
#> [1] "GB18030"
# korean text ko_utf8 <- read_file(file.path(ex_path, "ko.txt")) print(ko_utf8)
#> [1] "나는 유리를 먹을 수 있어요. 그래도 아프지 않아요\n"
detect_str_enc(iconv(ko_utf8, "utf8", "uhc"))
#> [1] "UHC"
detect_str_enc(iconv(ko_utf8, "utf8", "iso-2022-kr"))
#> [1] "ISO-2022-KR"