前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Golang语言版的ip2long函数实例

Golang语言版的ip2long函数实例

作者头像
李海彬
发布2018-03-21 10:49:31
1.4K0
发布2018-03-21 10:49:31
举报
文章被收录于专栏:Golang语言社区

本文实例讲述了go语言版的ip2long函数。分享给大家供大家参考。具体分析如下:

代码语言:javascript
复制
这里介绍的go语言版的ip2long 函数不会对 IP 的合法性进行校验。

// 注意: 该函数不会对 IP 的合法性进行校验
func Ip2Long(ip string) (ips string) {
    var ip_pieces = strings.Split(ip, ".")
 ip_1, _ := strconv.ParseInt(ip_pieces[0], 10, 32)
 ip_2, _ := strconv.ParseInt(ip_pieces[1], 10, 32)
 ip_3, _ := strconv.ParseInt(ip_pieces[2], 10, 32)
 ip_4, _ := strconv.ParseInt(ip_pieces[3], 10, 32)
 var ip_bin string = fmt.Sprintf("%08b%08b%08b%08b", ip_1, ip_2, ip_3, ip_4)
 ip_int, _ := strconv.ParseInt(ip_bin, 2, 64)
 return
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2016-04-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Golang语言社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档