ID EN
String Functions

Encoding

R Base 3.6.2

Read or set the declared encodings for a character vector.

Syntax

R
Encoding(x)<p></p><p>Encoding(x) &lt;- value</p><p>enc2native(x)
enc2utf8(x)</p>

Arguments

Parameter Description
x A character vector.
value A character vector of positive length.

Return Value

A character vector. For enc2utf8 encodings are always marked: they are for enc2native in UTF-8 and Latin-1 locales.

Details

Character strings in R can be declared to be encoded in "latin1" or "UTF-8" or as "bytes". These declarations can be read by Encoding, which will return a character vector of values "latin1", "UTF-8" "bytes" or "unknown", or set, when value is recycled as needed and other values are silently treated as "unknown". ASCII strings will never be marked with a declared encoding, since their representation is the same in all supported encodings. Strings marked as "bytes" are intended to be non-ASCII strings which should be manipulated as bytes, and never converted to a character encoding (so writing them to a text file is supported only by writeLines(useBytes = TRUE)). enc2native and enc2utf8 convert elements of character vectors to the native encoding or UTF-8 respectively, taking any marked enc

Examples

Example
R
# NOT RUN {
## x is intended to be in latin1
x <- "fa\xE7ile"
Encoding(x)
Encoding(x) <- "latin1"
x
xx <- iconv(x, "latin1", "UTF-8")
Encoding(c(x, xx))
c(x, xx)
Encoding(xx) <- "bytes"
xx # will be encoded in hex
cat("xx = ", xx, "\n", sep = "")
# }