IP地址与UINT32之间的转换

5年以前  |  阅读数:1103 次  |  编程语言:Golang 
package common

import (
    "encoding/binary"
    "net"
)

func IP2Long(ipstr string) uint32 {
    ip := net.ParseIP(ipstr)
    if ip == nil {
        return 0
    }
    ip = ip.To4()
    return binary.BigEndian.Uint32(ip)
}

func Long2IP(ipLong uint32) string {
    ipByte := make([]byte, 4)
    binary.BigEndian.PutUint32(ipByte, ipLong)
    ip := net.IP(ipByte)
    return ip.String()
}

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8