Is there a standard function to convert an IP address, say, 127.0.0.1 to a tuple,i.e. {127,0,0,1}?
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}}
: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.
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.
Thank you, @NobbZ.
It is clear to me now.
In case anyone else lands here via a web search you can run the inverse with :inet.ntoa/1
iex(1)> :inet.ntoa({127, 0, 0, 1})
~c"127.0.0.1"
(~c"127.0.0.1"
is the updated string representation of '127.0.0.1'
. They’re both charlists)