当前位置:网站首页>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 .
边栏推荐
- Jestson Nano 从tftp服务器下载更新kernel和dtb
- LeetCode每日一题(1162. As Far from Land as Possible)
- Equality judgment of long type
- Leetcode daily question (2090. K radius subarray averages)
- Jestson nano downloads updated kernel and DTB from TFTP server
- Leetcode daily question (516. long palindromic subsequence)
- Filter comments to filter out uncommented and default values
- Flink learning notes (10) Flink fault tolerance mechanism
- Integrated use of interlij idea and sonarqube
- Construction of simple database learning environment
猜你喜欢

Spark 概述

Spark 集群安装与部署

Common software open source protocols

Hudi 快速体验使用(含操作详细步骤及截图)

Hudi学习笔记(三) 核心概念剖析

Trial of the combination of RDS and crawler

The rise and fall of mobile phones in my perspective these 10 years

Crawler career from scratch (I): crawl the photos of my little sister ① (the website has been disabled)
![[kotlin learning] control flow of higher-order functions -- lambda return statements and anonymous functions](/img/a3/b442508af9b059d279cffb34dee9bf.png)
[kotlin learning] control flow of higher-order functions -- lambda return statements and anonymous functions

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
随机推荐
Call the contents of Excel cells opened at the same time - button line feed
Powerdesign reverse wizard such as SQL and generates name and comment
LeetCode每日一题(516. Longest Palindromic Subsequence)
The number of weak characters in the game (1996)
Common software open source protocols
[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 4 --blinker_ DHT_ WiFi (lighting technology app control + temperature and humidity data app display)
Numerical analysis notes (I): equation root
Leetcode daily question (2090. K radius subarray averages)
About the configuration of vs2008+rade CATIA v5r22
Analysis of the implementation principle of an open source markdown to rich text editor
IDEA 中使用 Hudi
DSP data calculation error
Hudi 快速体验使用(含操作详细步骤及截图)
LeetCode每日一题(968. Binary Tree Cameras)
The idea of compiling VBA Encyclopedia
LeetCode每日一题(985. Sum of Even Numbers After Queries)
Nodemcu-esp8266 development board to build Arduino ide development environment
基于opencv实现桌面图标识别
Solve editor MD uploads pictures and cannot get the picture address
