[Go] go语言版的ip2long 函数 →→→→→进入此内容的聊天室

来自 , 2020-12-11, 写在 Go, 查看 161 次.
URL http://www.code666.cn/view/b4631d19
  1. // 注意: 该函数不会对 IP 的合法性进行校验
  2. func Ip2Long(ip string) (ips string) {
  3.     var ip_pieces = strings.Split(ip, ".")
  4.         ip_1, _ := strconv.ParseInt(ip_pieces[0], 10, 32)
  5.         ip_2, _ := strconv.ParseInt(ip_pieces[1], 10, 32)
  6.         ip_3, _ := strconv.ParseInt(ip_pieces[2], 10, 32)
  7.         ip_4, _ := strconv.ParseInt(ip_pieces[3], 10, 32)
  8.  
  9.         var ip_bin string = fmt.Sprintf("%08b%08b%08b%08b", ip_1, ip_2, ip_3, ip_4)
  10.  
  11.         ip_int, _ := strconv.ParseInt(ip_bin, 2, 64)
  12.  
  13.         return
  14. }
  15. //go/4390

回复 "go语言版的ip2long 函数"

这儿你可以回复上面这条便签

captcha