当前位置:网站首页>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 .
边栏推荐
- Leetcode daily question (2109. adding spaces to a string)
- [CSDN]C1训练题解析_第四部分_Web进阶
- Win10 install elk
- Derivation of Fourier transform
- Make the most basic root file system of Jetson nano and mount NFS file system on the server
- Hudi 数据管理和存储概述
- ERROR: certificate common name “*.” doesn’t match requested ho
- 一款开源的Markdown转富文本编辑器的实现原理剖析
- Esp32 at command does not respond
- LeetCode每日一题(516. Longest Palindromic Subsequence)
猜你喜欢

Crawler career from scratch (IV): climb the bullet curtain of station B through API

Flink学习笔记(八)多流转换

【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数

文件系统中的目录与切换操作

Trial of the combination of RDS and crawler

Spark overview
![[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?](/img/fc/5c71e6457b836be04583365edbe08d.png)
[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?

NPM install installation dependency package error reporting solution

Win10安装ELK
![[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords](/img/ee/d982fd9e1f2283e09ad1a81d0b61b5.png)
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
随机推荐
1300. sum of varied array closed to target
Jestson nano custom root file system creation (supports the smallest root file system of NVIDIA Graphics Library)
Win10 quick screenshot
Starting from 0, use pnpm to build a demo managed by monorepo
Jestson nano downloads updated kernel and DTB from TFTP server
Implementing distributed lock with redis
Esp32 at command does not respond
Flink学习笔记(八)多流转换
CATIA automation object architecture - detailed explanation of application objects (III) systemservice
[solution to the new version of Flink without bat startup file]
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
Solve editor MD uploads pictures and cannot get the picture address
Common software open source protocols
Modify idea code
1922. Count Good Numbers
Leetcode daily question (2305. fair distribution of cookies)
Hudi quick experience (including detailed operation steps and screenshots)
IDEA 中使用 Hudi
Call the contents of Excel cells opened at the same time - button line feed
Hudi learning notes (III) analysis of core concepts
