Binary to string conversion

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Binary to string conversion

Abhishek Ranjan-2

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>>

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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Binary to string conversion

Hugo Mills-2
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
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions


--
Hugo Mills             | There are two hard problems in computer science:
hugo@... carfax.org.uk | Cache invalidation, naming things, and off-by-one
http://carfax.org.uk/  | errors.
PGP: E2AB1DE4          |

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

signature.asc (853 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Binary to string conversion

Abhishek Ranjan-2
I had a look at the unicode library of erlang and it contained following BIFs

bin_is_7bit/1
bom_to_encoding/1
characters_to_binary/1
characters_to_binary/2
characters_to_binary/3
characters_to_binary_int/2
characters_to_list/1
characters_to_list/2
characters_to_list_int/2
encoding_to_bom/1
module_info/0
module_info/1

But what I have is already a binary and I need it to convert into string,or given the erlang's logic of storing string as a list of characters I need binary to list converter but there isn't any as it looks like to me.

Please share suggestions.

> On May 10, 2017 at 4:37 PM Hugo Mills <[hidden email]> wrote:
>
>
> 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
> > [hidden email]
> > http://erlang.org/mailman/listinfo/erlang-questions
>
>
> --
> Hugo Mills | There are two hard problems in computer science:
> hugo@... carfax.org.uk | Cache invalidation, naming things, and off-by-one
> http://carfax.org.uk/ | errors.
> PGP: E2AB1DE4 |

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Binary to string conversion

Attila Rajmund Nohl
Hello!

Have you actually tried any of them? I presume this is what you need:

5> B= <<97,0,109,0,97,0,110,0>>.
<<97,0,109,0,97,0,110,0>>
9> unicode:characters_to_list(B, {utf16, little}).
"aman"


2017-05-10 13:36 GMT+02:00 Abhishek Ranjan <[hidden email]>:

> I had a look at the unicode library of erlang and it contained following
> BIFs
>
> bin_is_7bit/1
> bom_to_encoding/1
> characters_to_binary/1
> characters_to_binary/2
> characters_to_binary/3
> characters_to_binary_int/2
> characters_to_list/1
> characters_to_list/2
> characters_to_list_int/2
> encoding_to_bom/1
> module_info/0
> module_info/1
>
> But what I have is already a binary and I need it to convert into string,or
> given the erlang's logic of storing string as a list of characters I need
> binary to list converter but there isn't any as it looks like to me.
>
> Please share suggestions.
>
>
>> On May 10, 2017 at 4:37 PM Hugo Mills <[hidden email]> wrote:
>>
>>
>> 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
>> > [hidden email]
>> > http://erlang.org/mailman/listinfo/erlang-questions
>>
>>
>> --
>> Hugo Mills | There are two hard problems in computer science:
>> hugo@... carfax.org.uk | Cache invalidation, naming things, and off-by-one
>> http://carfax.org.uk/ | errors.
>> PGP: E2AB1DE4 |
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Binary to string conversion

Abhishek Ranjan-2
I tried

bin_is_7bit/1
bom_to_encoding/1
encoding_to_bom/1

and didn't try the ones starting with characters keyword as I had this doubt regarding them
that I have a binary not a character at hand already

But thank you for the reply

> On May 10, 2017 at 5:49 PM Attila Rajmund Nohl <[hidden email]> wrote:
>
>
> Hello!
>
> Have you actually tried any of them? I presume this is what you need:
>
> 5> B= <<97,0,109,0,97,0,110,0>>.
> <<97,0,109,0,97,0,110,0>>
> 9> unicode:characters_to_list(B, {utf16, little}).
> "aman"
>
>
> 2017-05-10 13:36 GMT+02:00 Abhishek Ranjan <[hidden email]>:
> > I had a look at the unicode library of erlang and it contained following
> > BIFs
> >
> > bin_is_7bit/1
> > bom_to_encoding/1
> > characters_to_binary/1
> > characters_to_binary/2
> > characters_to_binary/3
> > characters_to_binary_int/2
> > characters_to_list/1
> > characters_to_list/2
> > characters_to_list_int/2
> > encoding_to_bom/1
> > module_info/0
> > module_info/1
> >
> > But what I have is already a binary and I need it to convert into string,or
> > given the erlang's logic of storing string as a list of characters I need
> > binary to list converter but there isn't any as it looks like to me.
> >
> > Please share suggestions.
> >
> >
> >> On May 10, 2017 at 4:37 PM Hugo Mills <[hidden email]> wrote:
> >>
> >>
> >> 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
> >> > [hidden email]
> >> > http://erlang.org/mailman/listinfo/erlang-questions
> >>
> >>
> >> --
> >> Hugo Mills | There are two hard problems in computer science:
> >> hugo@... carfax.org.uk | Cache invalidation, naming things, and off-by-one
> >> http://carfax.org.uk/ | errors.
> >> PGP: E2AB1DE4 |
> >
> > _______________________________________________
> > erlang-questions mailing list
> > [hidden email]
> > http://erlang.org/mailman/listinfo/erlang-questions
> >

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Binary to string conversion

Raimo Niskanen-2
On Wed, May 10, 2017 at 06:07:56PM +0530, Abhishek Ranjan wrote:
> I tried
>
> bin_is_7bit/1
> bom_to_encoding/1
> encoding_to_bom/1
>
> and didn't try the ones starting with characters keyword as I had this doubt regarding them
> that I have a binary not a character at hand already

Read the documentation:
        http://erlang.org/doc/man/unicode.html
        http://erlang.org/doc/man/unicode.html#characters_to_list-1

It actually explains things...


>
> But thank you for the reply
>


--

/ Raimo Niskanen, Erlang/OTP, Ericsson AB
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Binary to string conversion

Abhishek Ranjan-2
Sure, thanks .

Regards,
Abhishek

> On May 10, 2017 at 6:56 PM Raimo Niskanen <[hidden email]> wrote:
>
>
> On Wed, May 10, 2017 at 06:07:56PM +0530, Abhishek Ranjan wrote:
> > I tried
> >
> > bin_is_7bit/1
> > bom_to_encoding/1
> > encoding_to_bom/1
> >
> > and didn't try the ones starting with characters keyword as I had this doubt regarding them
> > that I have a binary not a character at hand already
>
> Read the documentation:
> http://erlang.org/doc/man/unicode.html
> http://erlang.org/doc/man/unicode.html#characters_to_list-1
>
> It actually explains things...
>
>
> >
> > But thank you for the reply
> >
>
>
> --
>
> / Raimo Niskanen, Erlang/OTP, Ericsson AB
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions