Home > Backend Development > C++ > How to Efficiently Convert IPv4 Addresses to Integers and Vice Versa in C#?

How to Efficiently Convert IPv4 Addresses to Integers and Vice Versa in C#?

Patricia Arquette
Release: 2025-01-08 13:22:41
Original
658 people have browsed it

How to Efficiently Convert IPv4 Addresses to Integers and Vice Versa in C#?

Conversion of IPv4 address to integer in C#

Question:

How to efficiently convert an IPv4 address to an integer in C#? Additionally, please provide a method to perform the reverse transformation.

Answer:

In C#, IPv4 addresses are essentially represented as 32-bit unsigned integers. So, technically, no explicit conversion is required. However, to get the underlying integer value you can use the following steps:

  1. Get the Address property: Access the Address property of the IPAddress object corresponding to the IPv4 address. This property returns an Int64 value, a 32-bit unsigned integer representing the address.
  2. Swapping byte order (optional): The Address property returns an integer value in network byte order, which may not match the native byte order of the computer architecture. If necessary, you can use the IPAddress.NetworkToHostOrder method to swap bytes to obtain host byte order.

For example, to convert the IPv4 address "64.233.187.99" to an integer, you would use the following code:

<code class="language-csharp">long intValue = (long)(uint)IPAddress.NetworkToHostOrder(
                 (int)IPAddress.Parse("64.233.187.99").Address);</code>
Copy after login

Similarly, to convert an integer back to an IPv4 address, you can use the following steps:

  1. Create an IPAddress object: Create an IPAddress object using the IPAddress constructor and an integer value.
  2. Using the ToString method: Call the ToString method on the IPAddress object to get the string representation of the IPv4 address.

For example, to convert the integer 1089059683 back to an IPv4 address, you would use the following code:

<code class="language-csharp">string addressString = IPAddress.Parse(1089059683.ToString()).ToString(); // 注意这里需要将整数转换为字符串再进行解析
```  或者更简洁的写法:

```csharp
string addressString = new IPAddress(1089059683).ToString(); // 直接使用IPAddress构造函数</code>
Copy after login

Please note that in the second piece of code, the IPAddress object is constructed directly using integers as parameters without explicit conversion to strings. Both methods can achieve the goal, and the second method is more concise and efficient.

The above is the detailed content of How to Efficiently Convert IPv4 Addresses to Integers and Vice Versa in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template