当前位置:网站首页>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 .
边栏推荐
- Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 1 -- establishment of engineering template -template
- Esp32 at command does not respond
- PowerDesigner does not display table fields, only displays table names and references, which can be modified synchronously
- Spark structured stream writing Hudi practice
- unbuntu(debian)下TFTP服务器搭建及测试
- [kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?
- Flink learning notes (10) Flink fault tolerance mechanism
- Notes on numerical analysis (II): numerical solution of linear equations
- Crawler career from scratch (V): detailed explanation of re regular expression
- Flink学习笔记(八)多流转换
猜你喜欢
![[kotlin learning] classes, objects and interfaces - define class inheritance structure](/img/66/34396e51c59504ebbc6b6eb9831209.png)
[kotlin learning] classes, objects and interfaces - define class inheritance structure

PolyWorks script development learning notes (III) -treeview advanced operation

Vscode编辑器右键没有Open In Default Browser选项

PolyWorks script development learning notes (I) - script development environment

Flink learning notes (IX) status programming

LeetCode每日一题(1162. As Far from Land as Possible)

Solve the problem of disordered code in vscode development, output Chinese and open source code

Apply for domain name binding IP to open port 80 record

Spark 概述

数字身份验证服务商ADVANCE.AI顺利加入深跨协 推进跨境电商行业可持续性发展
随机推荐
LeetCode每日一题(2212. Maximum Points in an Archery Competition)
LeetCode每日一题(1162. As Far from Land as Possible)
Leetcode daily question (516. long palindromic subsequence)
Leetcode daily question (1162. as far from land as possible)
[kotlin learning] classes, objects and interfaces - define class inheritance structure
There is no open in default browser option in the right click of the vscade editor
Long类型的相等判断
Leetcode daily question (931. minimum falling path sum)
Spark 集群安装与部署
PolyWorks script development learning notes (III) -treeview advanced operation
PolyWorks script development learning notes (I) - script development environment
Quickly use markdown to edit articles
Analysis of the implementation principle of an open source markdown to rich text editor
LeetCode每日一题(1362. Closest Divisors)
WARNING: You are using pip ; however. Later, upgrade PIP failed, modulenotfounderror: no module named 'pip‘
Leetcode daily question (2212. maximum points in an archery competition)
Spark 概述
文件系统中的目录与切换操作
LeetCode每日一题(1856. Maximum Subarray Min-Product)
Hudi quick experience (including detailed operation steps and screenshots)
