Friday, November 19, 2010

Converting an Integer IP to ASCII in Erlang

As far as I can tell the base Erlang distribution doesn't have a function to convert an integer IP to an ASCII dotted quad. It has inet_parse:ntoa, but that function expects a 4-part tuple, not an integer. Surprisingly, Google is also remarkably quiet on the subject. So here, for posterity, is what I came up with:

inet_ntoa(IntIP) ->
    string:join(
        lists:map(
            fun(X) -> integer_to_list(X) end,
            binary_to_list(<<IntIP:32>>)
        ),
        "."
    ).

0 Comments:

Post a Comment

<< Home

Blog Information Profile for gg00