Matthew
Newbie with UnicodeConversionError
Hello, I am new to Elixir and so far finding it awesome. However I have just stumbled across a problem with non ascii strings. Please see below to see the error message I am getting:
iex(4)> "하이"
** (UnicodeConversionError) invalid encoding starting at <<199, 207, 192, 204, 34, 10>>
(elixir) lib/string.ex:1801: String.to_charlist/1
iex(4)>
I note that I am using Windows 10 and running iex from a Cmder shell (the same error occurs in windows cmd and in powershell).
Any suggestions for how I can fix this? (Besides using developing on a linux system
)
Most Liked
kip
Actually you can: String.codepoints/1 will decode what it can and show you the code points it can’t.
iex> x = <<160, 73, 100, 111, 119, 117, 32, 84, 97, 121, 108, 111, 114, 32, 83, 60, 47,
99, 111, 110, 116, 97, 99, 116, 45, 97, 100, 100, 114, 101, 115, 115, 62, 10>>
iex> String.codepoints x
[
<<160>>,
"I",
"d",
"o",
"w",
"u",
" ",
"T",
"a",
"y",
"l",
"o",
"r",
" ",
"S",
"<",
"/",
"c",
"o",
"n",
"t",
"a",
"c",
"t",
"-",
"a",
"d",
"d",
"r",
"e",
"s",
"s",
">",
"\n"
]
michalmuskala
While I have very little windows experience, I would expect this error to be caused by shell being not in unicode encoding.
In general any unicode issues on windows related to the shell were solved by using the graphical iex --werl shell.
Marcus
You can try to use the raw representation of the string.
iex(30)> i "하이"
...
Raw representation
<<237, 149, 152, 236, 157, 180>>
...
iex(31)> i <<237, 149, 152, 236, 157, 180>>
Term
"하이"
...
Raw representation
<<237, 149, 152, 236, 157, 180>>
...
iex(32)> str = <<237, 149, 152, 236, 157, 180>>
"하이"
iex(33)>
As you can see your string has just the wrong encoding.
iex(43)> String.to_charlist(<<199, 207, 192, 204, 34, 10>>)
** (UnicodeConversionError) invalid encoding starting at <<199, 207, 192, 204, 34, 10>>
(elixir) lib/string.ex:1801: String.to_charlist/1
iex(43)>








