It looks like UTF16 BE bytes in printable ASCII range are represented as ASCII characters (e.g. P instead of \\120), and at least some UTF16 BE bytes in ASCII control character range use the usual escape codes (e.g. \\b meaning ASCII backspace).
So encoding might also be using some other escapes like \\n for \\012 (line feed) or \\r for \\015 (carriage return).
Unfortunately I wasn’t able to have declare an equivalent literal value in Elixir; but, as long as your string is in the expected format, the same pattern should work.
If, however, the double-backslashes represent actual backslashes in the original string and are not the result of printing / inspecting it, then they’ll need to be unescaped first.
Now, as far as unescaping the string - if needed - this seems to do the trick:
def unescape(data) do
charlist = String.to_charlist(data)
erlang_literal = '"#{charlist}"'
{:ok, [{:string, _, unescaped_charlist}], _} = :erl_scan.string(erlang_literal)
List.to_string(unescaped_charlist)
end
What it does is transform the string into a charlist containing an Erlang “string expression”, which it proceeds to parse - and so I wouldn’t trust it with untrusted input without further investigation.