当前位置:网站首页>Convert IP address to int
Convert IP address to int
2022-07-03 09:37:00 【zhyg_ three hundred and twenty-one】
Sometimes there's a need like this , Need to put IP Address conversion into integer storage , Convert to when reading IP Address format
1、 take IP String conversion to integer save
The code is as follows :
string address; int intAddress; string ipAddress; while ((address = Console.ReadLine()) != "\\0") { // take IP Address conversion to byte array byte[] IPArr = IPAddress.Parse(address).GetAddressBytes(); // Display the contents stored in the byte array , The result is 16 Base display Console.WriteLine(BitConverter.ToString(IPArr, 0, IPArr.Length)); // Convert byte array to integer intAddress = BitConverter.ToInt32(IPArr, 0); Console.WriteLine(intAddress); } Console.ReadLine();
Running results :
C0-A8-0A-11 yes IPArr Convert to string Output result of type , It's also 192 168 10 17 Of 16 Base number
285911232 yes IPArr The result of converting to an integer ,C0 It's the lowest , That is to say, the integer is composed of 11-0A-A8-C0 convert to 10 Hexadecimal come , You can try !
2、 take IP The converted integer array is converted to IP Format
It's actually the reverse process of the first part , First convert integer to byte array , Then convert the byte array to IP
The code is as follows :
// Convert an integer to IP ipAddress = new IPAddress(BitConverter.GetBytes(intAddress)).ToString();
Running results :
The last line is the original IP
3、 summary
The two processes are mutually inverse ,IP Turn integer : utilize IPAddress.GetAddressBytes(), obtain IP Byte array format , And then use it BitConverter.ToInt32(), To convert to Int. The algorithm involved , It's nothing more than converting a byte array into 16 Base number , Then the first value of the array is converted into 10 Hexadecimal numbers get integer data ; Integer rotation IP Turn this process around .
边栏推荐
- 【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
- Jestson Nano 从tftp服务器下载更新kernel和dtb
- Hudi学习笔记(三) 核心概念剖析
- LeetCode每日一题(1024. Video Stitching)
- 307. Range Sum Query - Mutable
- Flink学习笔记(九)状态编程
- Jestson nano downloads updated kernel and DTB from TFTP server
- Global KYC service provider advance AI in vivo detection products have passed ISO international safety certification, and the product capability has reached a new level
- Nodemcu-esp8266 development board to build Arduino ide development environment
- Starting from 0, use pnpm to build a demo managed by monorepo
猜你喜欢
Hudi integrated spark data analysis example (including code flow and test results)
Spark 概述
PolyWorks script development learning notes (I) - script development environment
The rise and fall of mobile phones in my perspective these 10 years
Overview of image restoration methods -- paper notes
Detailed steps of windows installation redis
Apply for domain name binding IP to open port 80 record
Alibaba cloud notes for the first time
Hudi学习笔记(三) 核心概念剖析
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 5 --blinker_ MIOT_ MULTI_ Outside (lighting technology app + Xiaoai classmate control socket multiple jacks)
随机推荐
About the configuration of vs2008+rade CATIA v5r22
Jestson nano custom root file system creation (supports the smallest root file system of NVIDIA Graphics Library)
Modify idea code
Leetcode daily question (2090. K radius subarray averages)
Leetcode daily question (2305. fair distribution of cookies)
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 4 --blinker_ DHT_ WiFi (lighting technology app control + temperature and humidity data app display)
LeetCode每日一题(2232. Minimize Result by Adding Parentheses to Expression)
[CSDN]C1训练题解析_第四部分_Web进阶
1922. Count Good Numbers
软件测试工程师是做什么的 通过技术测试软件程序中是否有漏洞
Please tell me how to set vscode
解决Editor.md上传图片获取不到图片地址问题
文件系统中的目录与切换操作
Overview of image restoration methods -- paper notes
Flink learning notes (VIII) multi stream conversion
从0开始使用pnpm构建一个Monorepo方式管理的demo
LeetCode每日一题(2305. Fair Distribution of Cookies)
Idea uses the MVN command to package and report an error, which is not available
LeetCode每日一题(2090. K Radius Subarray Averages)
Hudi 快速体验使用(含操作详细步骤及截图)