3
$\begingroup$

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)")

$\endgroup$
  • 1
    $\begingroup$ Try CharacterEncoding -> "Unicode" instead. $\endgroup$ – MarcoB Oct 1 '15 at 1:38
  • $\begingroup$ That works, thanks. Am I mistaken that UTF8 should work, though? $\endgroup$ – ZachB Oct 1 '15 at 1:39
  • $\begingroup$ Ugh. See edit; I over-simplified my problem in my original question. $\endgroup$ – ZachB Oct 1 '15 at 1:43
  • $\begingroup$ 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. $\endgroup$ – MarcoB Oct 1 '15 at 1:45
  • $\begingroup$ 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 :-) $\endgroup$ – MarcoB Oct 1 '15 at 2:01
1
$\begingroup$

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"]

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.