How can I re-encode a text file (or string) so that non-ASCII characters will be represented using Mathematica's standard names?
For example, if the original contents of the file was
xαy
then the re-encoded file should contain
x\[Alpha]y
Using the CharacterEncoding "Mathematica7" comes close, but it is too aggressive: it also encodes ASCII characters like x and y:
ExportString["xαy", "Text", CharacterEncoding -> "Mathematica7"]
(* "\\170\\[Alpha]\\171" *)
I do not want this. Update: Looking more closely, this is probably not the purpose of the "MathematicaN" character encodings.
I also do not want to change the contents of the file, only change its encoding. This means that re-wrapping lines, eliminating whitespace, changing new lines, etc. is not acceptable.
Why do I need this? I want to take a package file that is UTF-8 encoded and make it platform independent. How such a file gets read by Get depends on $CharacterEncoding, which may differ between computers.
"Package"? – Alexey Popkov Apr 30 '17 at 13:56ExportString[xαy, "Package"]returns a string withx\[Alpha]yas the last line. Isn't it what you need? – Alexey Popkov Apr 30 '17 at 13:58\to\\, convert any newline to\n, reformat for a certain width, etc. I do not want these. – Szabolcs Apr 30 '17 at 13:59HoldComplete(with a controlled$CharacterEncoding), then re-export it (both as"Package"). But this also has undesirable side effects: it drops all comments and it destroys code formatting (making the code almost unreadable). I would prefer to only touch non-ASCII characters, and simply replace them by their Mathematica-name. – Szabolcs Apr 30 '17 at 14:01"Package"(.m) is "Plain ASCII text format." – Alexey Popkov Apr 30 '17 at 14:03.mfile. I typed non-ASCII characters. Others have done the same because on both OS X and Linux UTF-8 works just fine with Mathematica. On Windows, sometimes it does and sometimes it doesn't. It depends on the settings. There are.mfiles around with non-ASCII encodings. I do not want to go through the file and fix everything manually. I want Mathematica to do it for me. – Szabolcs Apr 30 '17 at 14:05ExportString[StringReplace["xαy", "α" -> "\\[Alpha]"], "Text"]? – Alexey Popkov Apr 30 '17 at 14:08.mfile using the FE and re-saving it. This can also be automated. – Szabolcs Apr 30 '17 at 14:13PrivateLookupNameByCode[c] <> "]";StringJoin@ Map[fromCode]@ ToCharacterCode@ Import["myfile", "Text", CharacterEncoding -> "UTF-8"]``
– Szabolcs May 01 '17 at 08:27