# Exporting special characters

I'm trying to export a string with UTF8 characters, but...

ToString[{"a" -> "\[Lambda]"}, CharacterEncoding -> "UTF8"]
(* Out: "{a -> Î»}" *)


My FE $CharacterEncoding is set to "UTF8". This gives the right value: ToCharacterCode["\[Lambda]"] (* Out: {955} *)  How can I export these characters? Edit ToString doesn't behave the same as ExportString. @MarcoB's suggestion CharacterEncoding -> "Unicode" works for the former, but not the latter. ExportString[{"a" -> "\[Lambda]"}, "Text", CharacterEncoding -> "Unicode"] (* Out: "ÿþ\"\.00a\.00\"\.00 \.00-\.00>\.00 \.00\"\.00»\.03\"\.00" *)  ($Version: "10.0 for Microsoft Windows (64-bit) (December 4, 2014)")

• Try CharacterEncoding -> "Unicode" instead. – MarcoB Oct 1 '15 at 1:38
• That works, thanks. Am I mistaken that UTF8 should work, though? – ZachB Oct 1 '15 at 1:39
• Ugh. See edit; I over-simplified my problem in my original question. – ZachB Oct 1 '15 at 1:43
• My understanding is that "All characters are represented internally and in files using Unicode values." (see the "Notebooks" section of Some Notes on Internal Implementation). That's why I tried Unicode, and it seemed to work. Let me take another look at the new facet of your problem though. – MarcoB Oct 1 '15 at 1:45
• Zach thank you for the accept. I tried to add a possible rationale for what you saw in my answer. That was a weird one, but at least I learned something today :-) – MarcoB Oct 1 '15 at 2:01

I think your problem is with your "Text" specification, rather than with the CharacterEncoding specification. It appears that, when you use "Text", exporting / importing defaults to trying to work with plain text (e.g. ASCII), which fails in the case of special characters.

This shows that specifying "Text" is equivalent to asking for {"Text", "Plaintext"}:

ExportString[{"a" -> "\[Lambda]"}, {"Text", "Plaintext"}] ==
ExportString[{"a" -> "\[Lambda]"}, "Text"]

(* True *)


The solution is to use the "String" element of "Text" instead, which specifies a "raw byte string" (from the documentation on "Text", under "Elements" -> "Data representation elements")

ExportString[{"a" -> "\[Lambda]"}, {"Text", "String"}]


or more simply but equivalently:

ExportString[{"a" -> "\[Lambda]"}, "String"]