How to concat a list of integers to a string in Erlang?

亡梦爱人 提交于 2020-06-16 05:49:18

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!