问题
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?
回答1:
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).
回答2:
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"
来源:https://stackoverflow.com/questions/25859835/how-to-concat-a-list-of-integers-to-a-string-in-erlang