Go IP地址转数字
主程序
InetAton ip转数字 , 提供net.IP格式值参和字符串输值参示例
InetNtoa 数字转IP
package main
import (
"encoding/binary"
"fmt"
"net"
)
func InetAton(ip net.IP) uint32 {
if len(ip) == 16 {
return binary.BigEndian.Uint32(ip[12:16])
}
return binary.BigEndian.Uint32(ip)
}
func InetNtoa(nn uint32) net.IP {
ip := make(net.IP, 4)
binary.BigEndian.PutUint32(ip, nn)
return ip
}
func main() {
ipv4 := InetNtoa(3232235521)
ipint2 := InetAton(ipv4)
ipint := InetAton(net.ParseIP("192.168.0.1"))
fmt.Println(ipv4, ipint,ipint2)
}
输出: 192.168.0.1 3232235521 3232235521