Erlang string to IP coversion and IP to string conversion. Works for IPv6 string to Erlang IP tuple conversion and Erlang IP tule to IPv6 address string.

As far as I have described how to convert Erlang IP tuple into integer and visa versa, so I thought why do not continue this topic with one more "conversion" articles. Sure you can find this info in the Internet, but as far as I have already touched IP, probably even this article will not be the last one.

So, Erlang kernel has an undocumented module for dealing with ipv4 and ipv6 internet addresses: inet_parse. This is mentioned briefly in the inet documentation and I think that in programming most convenience way to learn and understand something are examples.

Here is a string to Erlang tuple conversion:

1> inet_parse:address("127.0.0.1").
{ok,{127,0,0,1}}
2> inet_parse:address("::1").
{ok,{0,0,0,0,0,0,0,1}}
3> inet_parse:address("300.400.500.600").
{error,einval}

And here is Erlang tuple to string conversion:

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"

Looks like this is not going to be my last IP related article, so follow my blog in twitter

Posted by:
Enjoyed this post? Share and Leave a comment below, thanks! :)
Tags: , , ,