orcaman
orcaman

Reputation: 6551

How to concat a list of integers to a string in Erlang?

I have this tuple that looks like this:

{127,0,0,1}

Now I want to pass that tuple as the string "127.0.0.1" to an external lib (a geo IP lib). What's the best way to convert this tuple to the string?

Upvotes: 2

Views: 490

Answers (2)

P_A
P_A

Reputation: 1818

You can always use inet_parse:ntoa/1:

1> inet_parse:ntoa({127,0,0,1}).
"127.0.0.1"
2> inet_parse:ntoa({0,0,0,0,0,0,0,1}).
"::1"

Upvotes: 6

BlackMamba
BlackMamba

Reputation: 10254

You can use this:

ip_to_string({I1, I2, I3, I4}) ->
    lists:concat([I1,".",I2,".",I3,".",I4]);
ip_to_string({v6, Addr}) ->
    inet_parse:ntoa(Addr).

Upvotes: 4

Related Questions