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.

Recommend

Five-minute technical talk | AI technology and the governance of "cyber violence"

Part 01 What is “cyberbullying”? "Cyber ​​vi...

What is Wi-Fi and why is it so important?

The ubiquitous wireless technology Wi-Fi has beco...

Wireless charging is convenient, but how does it work?

In recent years, wireless charging has been widel...

JuHost: $2.99/month-1GB/20G SSD/1TB@100Mbps/Kowloon, Hong Kong

JuHost has released a regular November promotion,...

GSMA: Global 5G deployment will slow down due to the epidemic

On November 16, the Global System for Mobile Comm...