Dealing with :os.cmd with unicode characters

For some reason the following does not work in elixir:

:os.cmd("echo toto")

instead I read somewhere that this should be done

:os.cmd(to_charlist "echo toto")

which gives the expected result:

"toto\n"

but if now one wants to use non ascii character, like with this :

:os.cmd(to_charlist "echo à")

then the result becomes

[224, 10]

The question is : how can I have "à\n" as a result ?

You probably already do. But I’m guessing your source is not UTF, but rather some common Windows page, and 224 is the character you’re looking for. Because 10 is “\n”. When a char list is all ASCII chars it is displayed as a string, otherwise it is displayed as a list of byte values.

And the reason the first form does not work is that many older erlang commands only take char lists, not binaries. BTW, ‘echo toto’ would work just as well, as long as you’re using constants. (Of course if you have a binary arg passed in, then you’ll need that to_charlist call.)

2 Likes

Indeed, that was silly of me to think otherwise. I had an encoding issu that I had a hard time to solve and I was investigating all the possibilities. It turned out that Docker was causing the problem.

Thanks

1 Like

Although convenient most of the time, it is confusing sometimes that a single byte can so completely change the way the value is displayed…

And encoding issues. Ugh, just ugh. There are so many layers where it can go wrong…