Examples for 'base::iconv'


Convert Character Vector between Encodings

Aliases: iconv iconvlist

Keywords: character utilities

### ** Examples

## In principle, as not all systems have iconvlist
try(utils::head(iconvlist(), n = 50))
 [1] "1026"             "1046"             "1047"             "10646-1:1993"    
 [5] "437"              "500"              "500V1"            "850"             
 [9] "851"              "852"              "855"              "856"             
[13] "857"              "858"              "860"              "861"             
[17] "862"              "863"              "864"              "865"             
[21] "866"              "866NAV"           "869"              "874"             
[25] "8859_1"           "8859_2"           "8859_3"           "8859_4"          
[29] "8859_5"           "8859_6"           "8859_7"           "8859_8"          
[33] "8859_9"           "904"              "ANSI_X3.110"      "ANSI_X3.110-1983"
[37] "ANSI_X3.4"        "ANSI_X3.4-1968"   "ANSI_X3.4-1986"   "ARABIC"          
[41] "ARABIC7"          "ARMSCII-8"        "ARMSCII8"         "ASCII"           
[45] "ASMO_449"         "ASMO-708"         "BALTIC"           "BIG-5"           
[49] "BIG-FIVE"         "BIG5"            
## Not run: 
##D ## convert from Latin-2 to UTF-8: two of the glibc iconv variants.
##D iconv(x, "ISO_8859-2", "UTF-8")
##D iconv(x, "LATIN2", "UTF-8")
## End(Not run)

## Both x below are in latin1 and will only display correctly in a
## locale that can represent and display latin1.
x <- "fa\xE7ile"
Encoding(x) <- "latin1"
x
[1] "façile"
charToRaw(xx <- iconv(x, "latin1", "UTF-8"))
[1] 66 61 c3 a7 69 6c 65
xx
[1] "façile"
iconv(x, "latin1", "ASCII")          #   NA
[1] NA
iconv(x, "latin1", "ASCII", "?")     # "fa?ile"
[1] "fa?ile"
iconv(x, "latin1", "ASCII", "")      # "faile"
[1] "faile"
iconv(x, "latin1", "ASCII", "byte")  # "fa<e7>ile"
[1] "fa<e7>ile"
iconv(xx, "UTF-8", "ASCII", "Unicode") # "fa<U+00E7>ile"
[1] "fa<U+00E7>ile"
iconv(xx, "UTF-8", "ASCII", "c99")   # "fa\u00E7ile"
[1] "fa\\u00e7ile"
## Extracts from old R help files (they are nowadays in UTF-8)
x <- c("Ekstr\xf8m", "J\xf6reskog", "bi\xdfchen Z\xfcrcher")
Encoding(x) <- "latin1"
x
[1] "Ekstrøm"         "Jöreskog"        "bißchen Zürcher"
try(iconv(x, "latin1", "ASCII//TRANSLIT"))  # platform-dependent
[1] "Ekstrom"          "Joreskog"         "bisschen Zurcher"
iconv(x, "latin1", "ASCII", sub = "byte")
[1] "Ekstr<f8>m"            "J<f6>reskog"           "bi<df>chen Z<fc>rcher"
## and for Windows' 'Unicode'
str(xx <- iconv(x, "latin1", "UTF-16LE", toRaw = TRUE))
List of 3
 $ : raw [1:14] 45 00 6b 00 ...
 $ : raw [1:16] 4a 00 f6 00 ...
 $ : raw [1:30] 62 00 69 00 ...
iconv(xx, "UTF-16LE", "UTF-8")
[1] "Ekstrøm"         "Jöreskog"        "bißchen Zürcher"

[Package base version 4.2.3 Index]