【问题标题】:How to concat a list of integers to a string in Erlang?如何将整数列表连接到 Erlang 中的字符串?
【发布时间】:2020-03-22 12:30:01
【问题描述】:

我有一个如下所示的元组:

{127,0,0,1}

现在我想将该元组作为字符串 "127.0.0.1" 传递给外部库(地理 IP 库)。将此元组转换为字符串的最佳方法是什么?

【问题讨论】:

    标签: erlang


    【解决方案1】:

    您始终可以使用 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"
    

    【讨论】:

      【解决方案2】:

      你可以用这个:

      ip_to_string({I1, I2, I3, I4}) ->
          lists:concat([I1,".",I2,".",I3,".",I4]);
      ip_to_string({v6, Addr}) ->
          inet_parse:ntoa(Addr).
      

      【讨论】:

        猜你喜欢
        • 2012-03-05
        • 1970-01-01
        • 2021-08-07
        • 2015-01-21
        • 1970-01-01
        • 2018-10-15
        • 2017-01-17
        • 2018-06-19
        • 1970-01-01
        相关资源
        最近更新 更多