Emoji Encoding and Decoding erlang from list to binary

593 views Asked by At

I want this string to be parsed for VOIP Notification for iOS to binary string:

"{\"data\":{\"text\":\"❤️\"}}"

I expected the data printed on the Erlang shell to be:

<<"{\"data\":{\"text\":\"❤️\"}}">>

But when printed it shows:

{"data",[{"text",[10084,65039]}]}

I tried the solution from Encoding emoji in Erlang but it just gives me a bit string of the emoji which is:

{<<"data">>,{[{<<"text">>,<<100,39,15,254>>}]}}

How do I properly show it in Unicode in the Erlang shell?

1

There are 1 answers

0
Stratus3D On

In the first example you provide it is properly encoded, but it is not properly displayed. When you define strings using double quotes the string is actually stored as a list of character codes. In the case of the heart emoji [10084,65039] are the character codes. The Erlang shell isn't printing out the emoji itself, but rather the values of the bytes that represent the emoji.

In the second case, a binary string, you shouldn't need to do any special encoding of the binary, but you will need to tell Erlang to treat it as a UTF8 string. You can do that by specifying /utf8 at the end before the closing >>:

<<"{\"data\":{\"text\":\"❤️\"}}"/utf8>>.

If you want to see the emoji stored in strings and binaries like these displayed in the shell as actual emoji and not character codes, you will need to use the +pc option when starting the shell so it treats all unicode characters as printable characters:

erl +pc unicode

Setting it to unicode will cause emoji printed in the shell like this:

erlang erl shell emoji with unicode

http://erlang.org/doc/man/erl.html#id201195