I’m trying to use JavaScriptCore to do some string manipulation from the terminal, but I can’t get past some sort of character encoding problem.
If I run this in Terminal:
/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -e "print('héllo world')"
It should obviously print héllo world, but instead it prints héllo world. The same result happens if I run the print statement inside jsc interactive mode.
I know the gibberish text is an incorrect character encoding translation issue. The thing is, I can’t figure out how to tell jsc to use a particular encoding. I’ve experimented with changing $LANG for my shell, but that has no effect.
Where is the encoding issue happening and how do I fix it?
It seems like JavascriptCode does not support UTF8 input files (which is what Terminal is supplying here).
Interesting to note that
node.jsdoes process your example correctly.In your simple case, it may be possible to use the slightly extended 8 bit character set CP1252. I.e., if you put the above print command in a file
a.txtand useiconvto convert it:You can now run
jsc aa.txtand you get the desired output (héllo world).Another (inconvenient) option is to use Unicode code points directly in your strings. I.e., your example would look like this: