[Rd] iconv to UTF-16 encoding produces error due to embedded nulls (write.table with fileEncoding param)

nospam at altfeld-im.de nospam at altfeld-im.de
Tue Feb 16 18:25:14 CET 2016

If I execute the code from the "?write.table" examples section

  x <- data.frame(a = I("a \" quote"), b = pi)
  # (ommited code)
  write.csv(x, file = "foo.csv", fileEncoding = "UTF-16LE")

the resulting CSV file has a size of 6 bytes which is too short


The problem seems to be the iconv function:

  iconv("foo", to="UTF-16")


  Error in iconv("foo", to = "UTF-16"):
  embedded nul in string: '\xff\xfef\0o\0o\0'

In 2010 a (partial) patch for this problem was submitted:


Are there chances to fix this problem since it prevents writing Windows
UTF-16LE text files?

PS: This problem can be reproduced on Windows and Linux.


> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.3 LTS

 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods

loaded via a namespace (and not attached):
[1] tools_3.2.3

More information about the R-devel mailing list