3 Star 4 Fork 1

kelvins-io / common

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ip_int.go 763 Bytes
一键复制 编辑 原始数据 按行查看 历史
cristiane 提交于 2020-08-01 11:47 . 初始v1
package convert
import (
"bytes"
"strconv"
"strings"
)
func StringIpToInt(ipstring string) int64 {
ipSegs := strings.Split(ipstring, ".")
var ipInt int64 = 0
var pos uint = 24
for _, ipSeg := range ipSegs {
tempInt, _ := strconv.ParseInt(ipSeg, 10, 64)
tempInt = tempInt << pos
ipInt = ipInt | tempInt
pos -= 8
}
return ipInt
}
func IpIntToString(ipInt int64) string {
ipSegs := make([]string, 4)
var lenght int = len(ipSegs)
buffer := bytes.NewBufferString("")
for i := 0; i < lenght; i++ {
tempInt := ipInt & 0xFF
ipSegs[lenght-i-1] = strconv.FormatInt(tempInt, 10)
ipInt = ipInt >> 8
}
for i := 0; i < lenght; i++ {
buffer.WriteString(ipSegs[i])
if i < lenght-1 {
buffer.WriteString(".")
}
}
return buffer.String()
}
Go
1
https://gitee.com/kelvins-io/common.git
git@gitee.com:kelvins-io/common.git
kelvins-io
common
common
v1.1.7

搜索帮助