IP address conversion: conversion between numbers and strings

IP address conversion: conversion between numbers and strings

There are generally two formats for storing IP addresses in the IP address database, one is dotted decimal format (192.168.1.1), and the other is digital format (3232235777). In applications, it is often necessary to convert between these two formats.

[[260382]]

To solve this problem, I implemented a quick method for converting between the two in the exnet extension package:

  • func IP2Long(ip net.IP) (uint, error) IP2Long converts net.IP to a numeric value
  • func Long2IP(i uint) (net.IP, error) Long2IP converts the value to net.IP
  • func IPString2Long(ip string) (uint, error) IPString2Long converts the ip string to a numeric value
  • func Long2IPString(i uint) (string, error) Long2IPString converts the value to an IP string

Example of use:

  1. package main
  2.  
  3. import (
  4. "fmt"  
  5. "net"  
  6. "reflect"  
  7.  
  8. "github.com/thinkeridea/go-extend/exnet"  
  9. )
  10.  
  11. func main() {
  12. ip := "192.168.1.1"  
  13.  
  14. n, _ := exnet.IPString2Long(ip)
  15. s, _ := exnet.Long2IPString(n)
  16.  
  17. fmt.Println(n, s == ip)
  18.  
  19. Ip1 := net.ParseIP(ip) // You will get a 16-byte byte, mainly for compatibility with ipv6
  20. n, _ = exnet.IP2Long(Ip1)
  21.  
  22. Ip2, _ := exnet.Long2IP(n)
  23.  
  24. fmt.Println(n, reflect.DeepEqual(Ip1[12:], Ip2))
  25. }

So how do you convert a dotted decimal IP address into a number?

An IPv4 address has 4 bytes and looks like this:

  • MSB————–LSB
  • b4 b3 b2 b1

Each byte represents the range:

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

General formula: b4<<24 | b3<<16 | b2<<16 | b1

For example, 222.173.108.86

Conversion method: 222<<24 | 173<<16 | 108<<8 | 86 = 3735907414

For example, 1.0.1.1

Conversion method: 1<<24 | 0<<16 | 1<<8 | 1 = 16777473

The implementation in exnet is as follows:

  1. // IPString2Long converts the IP string into a numerical value
  2. func IPString2Long(ip string) (uint, error) {
  3. b := net.ParseIP(ip).To4()
  4. if b == nil {
  5. return 0, errors.New( "invalid ipv4 format" )
  6. }
  7.  
  8. return uint(b[3]) | uint(b[2])<<8 | uint(b[1])<<16 | uint(b[0])<<24, nil
  9. }

Just flip the logic of converting a numeric value to a string. This is implemented in exnet as follows:

  1. // Long2IPString converts the value to an IP string
  2. func Long2IPString(i uint) (string, error) {
  3. if i > math.MaxUint32 {
  4. return   "" , errors.New( "beyond the scope of ipv4" )
  5. }
  6.  
  7. ip := make(net.IP, net.IPv4len)
  8. ip[0] = byte(i >> 24)
  9. ip[1] = byte(i >> 16)
  10. ip[2] = byte(i >> 8)
  11. ip[3] = byte(i)
  12.  
  13. return ip.String(), nil
  14. }

<<:  When porting your number to another network, operators should first change their service mindset

>>:  The total investment of China Mobile, China Unicom and China Telecom does not exceed 34.2 billion! 5G cannot be swallowed in one go, so it needs to be eaten slowly.

TCP
TCP
Blog    

Recommend

Regarding the 6G satellite, I am "confused"

[[351012]] On November 6, a satellite named "...

Advantages and Challenges of 5G Network Slicing

The fifth generation of mobile communication syst...

...

Does Localhost necessarily mean Localhost?

[[405743]] We often use the localhost domain name...

5G means data center platforms must evolve

The foundation for seamless 5G implementation 5G ...

Double 11 Carnival, drink this bowl of "traffic control" soup

[[350322]] As the Double 11 shopping festival app...

Can Huawei reshape the Internet?

[[320457]] This article is reproduced from Leipho...

A brief comparison of two SR-TE implementation methods

1. Brief description of background technology Reg...

Ruijie Cloud Desktop has emerged as a new force. Why do users prefer it?

[51CTO.com original article] Speaking of players ...