[erlang-questions] Binary to string conversion

Hugo Mills hugo@REDACTED
Wed May 10 13:07:13 CEST 2017


On Wed, May 10, 2017 at 04:11:18PM +0530, Abhishek Ranjan wrote:
> I have set up a chat application using ejabberd(built using erlang) and Pidgin.
> 
> I have witnessed a proceeding where when I print out a message sent by the client to server on ejabberd it comes out as string 
> 
> For example,if I send a message as
> 
> aman
> 
> from the pidgin and print it inside the module containing ejabberd hook as CustomMod.erl using
> 
> > 
> >     Bin=xmpp:get_text(Packet#message.body),
> >     Name=binary_to_list(Bin),
> > 
> >     ?INFO_MSG("The value of Name  is ~p~n",[Name]), 
> > 
> 
> the output comes as "aman"
> 
> but I have a simple erlang module also for login as login.erl and it does not implement any ejabberd behavior, but is called from within CustomMod.erl  when I print this message out in this module it comes enclosed within these << >> but are separated by 0's like
> 
> aman becomes here as <<97,0,109,0,97,0,110,0>>

   That looks like little-endian UTF-16 to me. You probably need to do
some Unicode conversion on your binary.

   Hugo.

> which on doing binary_to_list(Variable) inside my login.erl   I get-> [97,0,109,0,97,0,110,0]
> 
> (where Variable is the value enclosed within <<>>)
> 
>  Can anyone tell me why is it happening and what is the way to get the value aman in login.erl  also?
> 
> 
> Regards,
> 
> Abhishek 

> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions


-- 
Hugo Mills             | There are two hard problems in computer science:
hugo@REDACTED carfax.org.uk | Cache invalidation, naming things, and off-by-one
http://carfax.org.uk/  | errors.
PGP: E2AB1DE4          |
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170510/4c3b588c/attachment.bin>


More information about the erlang-questions mailing list