代码
func ip_num_string_test() {
ip_str := "192.168.0.1"
byteip := net.ParseIP(ip_str).To4()
num := binary.BigEndian.Uint32(byteip)
zz := hex.EncodeToString(byteip)
fmt.Printf("byteip hex-encode %s \n", zz)
fmt.Printf("byteip %02x%02x%02x%02x\n", byteip[0], byteip[1], byteip[2], byteip[3])
fmt.Printf("Littleendian_num %x\n", num)
num++
x := make([]byte, 4)
binary.BigEndian.PutUint32(x, num)
new_ipstr := fmt.Sprintf("%d.%d.%d.%d\n", x[0], x[1], x[2], x[3])
fmt.Printf("New ip str %s\n", new_ipstr)
}
执行结果
root@ubuntu:~/go123/gendata
byteip hex-encode c0a80001
byteip c0a80001
Littleendian_num c0a80001
New ip str 192.168.0.2