英文:
Golang Net.IP to IPv6 (from MySQL) as Decimal(39,0) Conversion?
问题
我有一个将IPv4和IPv6地址存储为decimal(39,0)的数据库。我需要将Golang的Net.IP转换为这种格式。我已经为IPv4完成了以下转换:
func ipv4ToInt(IPv4Addr net.IP) int64 {
bits := strings.Split(IPv4Addr.String(), ".")
b0, _ := strconv.Atoi(bits[0])
b1, _ := strconv.Atoi(bits[1])
b2, _ := strconv.Atoi(bits[2])
b3, _ := strconv.Atoi(bits[3])
var sum int64
sum += int64(b0) << 24
sum += int64(b1) << 16
sum += int64(b2) << 8
sum += int64(b3)
return sum
}
我正在尝试对IPv6进行相同的转换:
func ipv6ToInt(IPv6Addr net.IP) Int128 {
bits := strings.Split(IPv6Addr.String(), ":")
var arr [4]int64
var arr1 [4]uint64
for i := 0; i < 4; i++ {
arr[i], _ = strconv.ParseInt(bits[i], 16, 64)
}
for i := 0; i < 4; i++ {
arr1[i], _ = strconv.ParseUint(bits[i], 16, 64)
}
int1 := arr[0]
for i := 0; i < 4; i++ {
int1 = (int1 << 16) + arr[i]
}
int2 := arr1[0]
for i := 0; i < 4; i++ {
int2 = (int2 << 16) + arr1[i]
}
var IPv6Int Int128
IPv6Int.H = int1
IPv6Int.L = int2
return IPv6Int
}
其中Int128定义如下:
type Int128 struct {
H int64
L uint64
}
结果应该如下所示:
42540578165168461141553663388954918914
来自IPv6地址:
2001:470:0:76::2
谢谢!
编辑,答案:
感谢#go-nuts频道的人们,答案如下:
func ipv6ToInt(IPv6Addr net.IP) *big.Int {
IPv6Int := big.NewInt(0)
IPv6Int.SetBytes(IPv6Addr)
return IPv6Int
}
对于IPv6,只需先执行IP.To4()即可。
英文:
I have a database that stores IPv4 and IPv6 addresses as decimal(39,0). I need to convert a Golang Net.IP to this format. I have done it for IPv4 as follows:
func ipv4ToInt(IPv4Addr net.IP) int64 {
bits := strings.Split(IPv4Addr.String(), ".")
b0, _ := strconv.Atoi(bits[0])
b1, _ := strconv.Atoi(bits[1])
b2, _ := strconv.Atoi(bits[2])
b3, _ := strconv.Atoi(bits[3])
var sum int64
sum += int64(b0) << 24
sum += int64(b1) << 16
sum += int64(b2) << 8
sum += int64(b3)
return sum
}
I am trying the same with IPv6:
func ipv6ToInt(IPv6Addr net.IP) Int128 {
bits := strings.Split(IPv6Addr.String(), ":")
var arr [4]int64
var arr1 [4]uint64
for i := 0; i < 4; i++ {
arr[i], _ = strconv.ParseInt(bits[i], 16, 64)
}
for i := 0; i < 4; i++ {
arr1[i], _ = strconv.ParseUint(bits[i], 16, 64)
}
int1 := arr[0]
for i := 0; i < 4; i++ {
int1 = (int1 << 16) + arr[i]
}
int2 := arr1[0]
for i := 0; i < 4; i++ {
int2 = (int2 << 16) + arr1[i]
}
var IPv6Int Int128
IPv6Int.H = int1
IPv6Int.L = int2
return IPv6Int
}
Where int128 is
type Int128 struct {
H int64
L uint64
}
The result should look like:
42540578165168461141553663388954918914
from the IPv6 addr:
2001:470:0:76::2
Thanks!
EDIT, ANSWER:
Thanks to the people in #go-nuts, the answer is as follows:
func ipv6ToInt(IPv6Addr net.IP) *big.Int {
IPv6Int := big.NewInt(0)
IPv6Int.SetBytes(IPv6Addr)
return IPv6Int
}
The same works for IPv6, just do IP.To4() first.
答案1
得分: 2
感谢#go-nuts中的人们,答案如下:
func ipv6ToInt(IPv6Addr net.IP) *big.Int {
IPv6Int := big.NewInt(0)
IPv6Int.SetBytes(IPv6Addr)
return IPv6Int
}
对于IPv4,同样的方法也适用,只需先使用IP.To4()
。
英文:
Thanks to the people in #go-nuts, the answer is as follows:
func ipv6ToInt(IPv6Addr net.IP) *big.Int {
IPv6Int := big.NewInt(0)
IPv6Int.SetBytes(IPv6Addr)
return IPv6Int
}
The same works for IPv4, just do IP.To4() first.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论