Conversion between IPv4 address and integer in C#
When dealing with IPv4 addresses, it is often necessary to convert them to integers or vice versa. C# provides several methods to perform these conversions.
To convert an IPv4 address to an integer, you can use the IPAddress
attribute of the class, which returns a 32-bit unsigned integer representing the IPv4 address. However, this property returns the address in network byte order, so the bytes need to be swapped to get host byte order. Address
<code class="language-csharp">long address = (long)(uint)IPAddress.NetworkToHostOrder( (int)IPAddress.Parse("64.233.187.99").Address);</code>
class: IPAddress
<code class="language-csharp">string ipAddress = new IPAddress((uint)IPAddress.HostToNetworkOrder( (int)address)).ToString();</code>
The above is the detailed content of How to Convert IPv4 Addresses to Integers and Vice Versa in C#?. For more information, please follow other related articles on the PHP Chinese website!