• 主页
  • Go
  • ChatOps
  • 归档
  • 关于
所有文章

  • 主页
  • Go
  • ChatOps
  • 归档
  • 关于

【Go】IP地址转换:数字与字符串之间的转换

2019-03-25

IP 地址库中 IP 地址的保存格式一般有两种,一种是点分十进制形式(192.168.1.1),另一种是数字形式(3232235777),应用中,经常需要在这两种格式之间做转换。

针对这一个问题我在 exnet 扩展包里面实现可两者的转换的快捷方法:

  • func IP2Long(ip net.IP) (uint, error) IP2Long 把 net.IP 转为数值
  • func Long2IP(i uint) (net.IP, error) Long2IP 把数值转为 net.IP
  • func IPString2Long(ip string) (uint, error) IPString2Long 把 ip 字符串转为数值
  • func Long2IPString(i uint) (string, error) Long2IPString 把数值转为 ip 字符串

使用示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main

import (
"fmt"
"net"
"reflect"

"github.com/thinkeridea/go-extend/exnet"
)

func main() {
ip := "192.168.1.1"

n, _ := exnet.IPString2Long(ip)
s, _ := exnet.Long2IPString(n)

fmt.Println(n, s == ip)

Ip1 := net.ParseIP(ip) // 会得到一个16字节的byte,主要为了兼容ipv6
n, _ = exnet.IP2Long(Ip1)

Ip2, _ := exnet.Long2IP(n)

fmt.Println(n, reflect.DeepEqual(Ip1[12:], Ip2))
}

那么是如何将点分十进制的IP地址转为数字?
IPv4 地址有4个字节,样式如下:
MSB————–LSB
b4 b3 b2 b1

每个字节表示的范围:

  • byte4: 4294967296(1<<32)
  • byte3: 16777216(1<<24)
  • byte2: 65536(1<<16)
  • byte1: 256(1<<8)

通用公式:b4<<24 | b3<<16 | b2<<8 | b1
例如,222.173.108.86
转换方法:222<<24 | 173<<16 | 108<<8 | 86 = 3735907414
再例如,1.0.1.1
转换方法:1<<24 | 0<<16 | 1<<8 | 1 = 16777473

exnet 中实现如下:

1
2
3
4
5
6
7
8
9
// IPString2Long 把ip字符串转为数值
func IPString2Long(ip string) (uint, error) {
b := net.ParseIP(ip).To4()
if b == nil {
return 0, errors.New("invalid ipv4 format")
}

return uint(b[3]) | uint(b[2])<<8 | uint(b[1])<<16 | uint(b[0])<<24, nil
}

把数值转换为字符串的逻辑翻转过来即可, exnet 中实现如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Long2IPString 把数值转为ip字符串
func Long2IPString(i uint) (string, error) {
if i > math.MaxUint32 {
return "", errors.New("beyond the scope of ipv4")
}

ip := make(net.IP, net.IPv4len)
ip[0] = byte(i >> 24)
ip[1] = byte(i >> 16)
ip[2] = byte(i >> 8)
ip[3] = byte(i)

return ip.String(), nil
}
赏

谢谢你请我吃糖果

支付宝
微信
  • 本文作者: 戚银(thinkeridea)
  • 本文链接: https://blog.thinkeridea.com/201903/go/ip2long.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处!
  • go
  • ip
  • net
  • ip2long
  • ip2number
  • go

扫一扫,分享到微信

微信分享二维码
【Go】获取用户真实的ip地址
【Go】go get 自动代理
目录,不存在的…
© 2021 戚银 京ICP备17038866号-1
Hexo Theme Yilia by Litten
  • 所有文章

tag:

  • chatops
  • devops
  • hubot
  • github
  • gitlab
  • slack
  • rocket.chat
  • linux
  • centos
  • VirtualBox
  • 读写锁
  • 死锁
  • net.http
  • http
  • pool
  • bytes
  • buffer
  • makeSlice
  • ioutil
  • go
  • strings
  • replace
  • unsafe
  • string
  • int
  • numerical
  • uintptr
  • proxy
  • git
  • shell
  • ip
  • net
  • ip2long
  • ip2number
  • client
  • RemoteAddr
  • compress
  • io
  • 压缩
  • substring
  • substr
  • utf8
  • 中文字符串截取
  • sync
  • once
  • OncePointer
  • BufferPool
  • strings.Replacer
  • closeOnce
  • slice
  • array
  • 缓存
  • 内存池
  • 日志
  • csv
  • join
  • round
  • half up
  • exmath
  • math