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

How to configure floating routing?

[[416937]] Experimental requirements ISP-1 and IS...

One million new 5G base stations will be built next year. Is that a lot?

The latest news shows that my country has built a...

Intel and XSKY Debut at 2019 China Data and Storage Summit

On December 3-4, the 2019 China Data and Storage ...

Most enterprise networks cannot handle big data loads

Enterprise IT can't keep up with the growing ...

Application of 5G in epidemic prevention and control in medical system

At the beginning of 2020, the COVID-19 epidemic w...

WOT Li Jian: The evolution of the eleme container platform

[51CTO.com original article] The Global Software ...

A Simple Explanation of Decentralized Applications

[[397123]] In this article, we will explain what ...

Obstacles to 5G rollout

While 5G has the potential to open up many exciti...

BGPTO Singapore $49/month dedicated server simple test

A few days ago, the blog shared the information t...