Is there a function to convert an IP address to a tuple, i.e. {127,0,0,1}?

Is there a standard function to convert an IP address, say, 127.0.0.1 to a tuple,i.e. {127,0,0,1}?

1 Like

The Erlang standard libraries include one that will convert a charlist. Perhaps it will meet your needs.

iex(123)> :inet.parse_address '127.0.0.1'
{:ok, {127, 0, 0, 1}}
5 Likes

:inet.parse_address/1 should do.

Sorry for not providing a link, for some reason I can’t get a link for it on mobile.

2 Likes

Many thanks @gregvaughn.
It worked

Many thanks, @NobbZ, for the solution.

I have a question…
why does the below not work?

:inet.parse_address “127.0.0.1”
{:error, :einval}

Because it is an Erlang function that does still only work on Erlang strings, aka charlists.

There has not been an update yet to allow for binary strings as well.

3 Likes

Thank you, @NobbZ.
It is clear to me now.