douxie2007
2012-06-09 14:08
浏览 42
已采纳

在Go中解析字符串中的多个整数?

sorry for the title , i don't know how to say that

i have a questions about this code

func ip2long(ip string) (ret int64) {
  p:= strings.Split(ip, ".")
  n, _:= strconv.Atoi(p[0])
  ret += int64(n)*16777216 
  n, _= strconv.Atoi(p[1])
  ret += int64(n)*65536 
  n, _= strconv.Atoi(p[2])
  ret += int64(n)*256
  n, _= strconv.Atoi(p[3])
  ret += int64(n)

  return 
}

I want to convert an ip address to integer number

you see I have wrote such ugly code

first retrive number from strconv.Atoi then convert it to int64

How to simplify this ?

图片转代码服务由CSDN问答提供 功能建议

很抱歉,我不知道怎么说

我对此代码有疑问

  func ip2long(ip string)(ret int64){
p:= strings.Split(ip,“。”)
n,  _:= strconv.Atoi(p [0])
 ret + = int64(n)* 16777216 
n,_ = strconv.Atoi(p [1])
 ret + = int64(n)* 65536 
n  ,_ = strconv.Atoi(p [2])
 ret + = int64(n)* 256 
n,_ = strconv.Atoi(p [3])
 ret + = int64(n)
 
 返回
} 
   
 
 

我想将IP地址转换为整数

您会看到我写了这么难看的代码

首先从strconv.Atoi中检索数字,然后将其转换为int64

如何简化此过程?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

相关推荐 更多相似问题