Pablisky
Pablisky

Reputation: 55

How can I get the ascii characters from a string in Erlang

I know i could get the ascii character from one letter just like this:

>Letter = "a",
>hd(Letter).
>97

But I need something like this, where all the ascii characters are concatenated:

>Letter = "abc",
>hd(Letter).
>979899

Besides that, I know the following fuction "returns" a list with all the ascii characters, but I can not assign it to a variable.

>io: format ( "~ w" [ "abc"]).
>[97,98,99]

Upvotes: 3

Views: 3543

Answers (1)

legoscia
legoscia

Reputation: 41528

You can use the function lists:flatmap/2:

> lists:flatmap(fun erlang:integer_to_list/1, "abc").
"979899"

It applies the given function to each element, and "flattens" the result, resulting in concatenation.

Upvotes: 2

Related Questions