【Go】IP地址转换:数字与字符串之间高效转换
转载:https://blog.thinkeridea.com/201903/go/ip2long.html
IP 地址库中 IP 地址的保存格式一般有两种,一种是点分十进制形式(192.168.1.1),另一种是数字形式(3232235777),应用中,经常需要在这两种格式之间做转换。
针对这一个问题我在 exnet 扩展包里面实现可两者的转换的快捷方法:
func IP2Long(ip net.IP) (uint, error)IP2Long 把 net.IP 转为数值func Long2IP(i uint) (net.IP, error)Long2IP 把数值转为 net.IPfunc IPString2Long(ip string) (uint, error)IPString2Long 把 ip 字符串转为数值func Long2IPString(i uint) (string, error)Long2IPString 把数值转为 ip 字符串
使用示例:
package main
import (
"fmt"
"net"
"reflect"
"github.com/thinkeridea/go-extend/exnet"
)
func main() {
ip := "192.168.1.1"
n, _ := exnet.IPString2Long(ip)
s, _ := exnet.Long2IPString(n)
fmt.Println(n, s == ip)
Ip1 := net.ParseIP(ip) // 会得到一个16字节的byte,主要为了兼容ipv6
n, _ = exnet.IP2Long(Ip1)
Ip2, _ := exnet.Long2IP(n)
fmt.Println(n, reflect.DeepEqual(Ip1[12:], Ip2))
}
那么是如何将点分十进制的IP地址转为数字?
IPv4 地址有4个字节,样式如下:
MSB————–LSB
b4 b3 b2 b1
每个字节表示的范围:
- byte4: 4294967296(1<<32)
- byte3: 16777216(1<<24)
- byte2: 65536(1<<16)
- byte1: 256(1<<8)
通用公式:b4<<24 | b3<<16 | b2<<16 | b1
例如,222.173.108.86
转换方法:222<<24 | 173<<16 | 108<<8 | 86 = 3735907414
再例如,1.0.1.1
转换方法:1<<24 | 0<<16 | 1<<8 | 1 = 16777473
exnet 中实现如下:
// IPString2Long 把ip字符串转为数值
func IPString2Long(ip string) (uint, error) {
b := net.ParseIP(ip).To4()
if b == nil {
return 0, errors.New("invalid ipv4 format")
}
return uint(b[3]) | uint(b[2])<<8 | uint(b[1])<<16 | uint(b[0])<<24, nil
}
把数值转换为字符串的逻辑翻转过来即可, exnet 中实现如下:
// Long2IPString 把数值转为ip字符串
func Long2IPString(i uint) (string, error) {
if i > math.MaxUint32 {
return "", errors.New("beyond the scope of ipv4")
}
ip := make(net.IP, net.IPv4len)
ip[0] = byte(i >> 24)
ip[1] = byte(i >> 16)
ip[2] = byte(i >> 8)
ip[3] = byte(i)
return ip.String(), nil
}
转载:
本文作者: 戚银(thinkeridea)
本文链接: https://blog.thinkeridea.com/201903/go/ip2long.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处!
【Go】IP地址转换:数字与字符串之间高效转换的更多相关文章
- C++中数字与字符串之间的转换 scanf string总结(复习必读)
1 string的scanf读入操作 C++里面控制台输入直接使用cin操作就可以了:或者getline(istringstream,string); 字符和数字加减就是字符的ASCII码和数字直接加 ...
- C++中数字与字符串之间的转换,别人的,
C++中数字与字符串之间的转换 1.字符串数字之间的转换 (1)string --> char * string str("OK"); char * p = st ...
- C++中数字与字符串之间的转换
原文地址:http://www.cnblogs.com/luxiaoxun/archive/2012/08/03/2621803.html 1.字符串数字之间的转换 (1)string --> ...
- C++中数字与字符串之间的转换(转)
http://www.cnblogs.com/luxiaoxun/archive/2012/08/03/2621803.html 1.字符串数字之间的转换 (1)string --> char ...
- C++中数字与字符串之间的转换(使用CString.Format或者sprintf)
1.字符串数字之间的转换 (1)string --> char * string str("OK"); char * p = str.c_str(); (2)char ...
- [C/C++] C/C++中数字与字符串之间的转换
在C中: 方法: 1.C标准库中的sprintf, sscanf 2.C标准库还提供了 atoi, atof, atol, atoll(C++11标准) 函数将字符串转换成int,double, lo ...
- js 中数字与字符串之间的转换
数字转换为字符串 var num = 123: 1.num.toString 2."" + num 3.String(num) 将数字转化为格式化后的字符串 num.toFixe ...
- boost-使用format和lexical_cast实现数字和字符串之间的转换
使用boost的format可以实现数字到string的格式化转换,boost的lexical_cast可以实现string到数值的转换,eg: #include "boost/format ...
- 数字与字符串之间的转换以及%f与%lf的输入输出用法区别
1.C++字符串与C字符串的转换: (1)string --> char * string str("OK"); strcpy(p,str.c_str());//p是char ...
随机推荐
- python pandas.DataFrame.append
1.使用append首先要注意的是,你要合并两个DataFrame的columns即列名是否是相同的,不相同的就会报错. 2.我们会发现DataFrame的列名是不能够重复的,而行名(index)是可 ...
- 20155205 郝博雅 Exp5 MSF基础应用
20155205 郝博雅 Exp5 MSF基础应用 一.实验目标 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 1.一个主动攻击实践,如ms08_0 ...
- How Does Closure Work in Javascript?
Simply, closure is the scope that it can visite and operate the variables outside of the function wh ...
- Servlet的创建二以及生命周期
之前说Servlet可以通过实现Servlet接口来创建,但是我们看到了,需要重写该接口中的所有方法. 创建方式二:Servlet的创建还可以继承抽象类GenericServlet并重写其中的抽象方法 ...
- linux sshd 登录不需要密码
ssh 安全外壳协议 协议22 linux 默认放行了 22 号接口 ssh 默认安装 自行安装 应该是 openssh-server ssh命令是 openssh ssh-keygen 生成密钥 生 ...
- ubuntu16 gitlab的简单安装
1.安装好ubuntu的ssh服务,使用xshell登录虚拟机 2.下载安装包: wget -c https://downloads-packages.s3.amazonaws.com/ubuntu ...
- 画PCB之电流与线宽的关系
来源:(多图) 超强整理!PCB设计之电流与线宽的关系http://www.51hei.com/bbs/dpj-39134-1.html 关于PCB线宽和电流的经验公式,关系表和软件网上都很多,本文把 ...
- 1.Spring AOP应用
首先咱们来了解一下具体的业务场景(这是个真实的项目的业务场景):具体的业务是这样的,现在系统中有六十多个主档(功能模块),每个主档都有新增.修改.删除功能,当我们在对每个主档做这些操作时需要对其记录日 ...
- mvc网站迁移.net core记录
接口return Json()时序列号小写的问题 在Startup.cs->ConfigureServices方法配置一下解决 public void ConfigureServices(ISe ...
- Android JNI 学习(九):Static Fields Api & Static Methods Api
一.Accessing Static Fields(访问静态域) 1. GetStaticFieldID jfieldIDGetStaticFieldID(JNIEnv *env, jclass cl ...