What is the equivalent of the inet_addr function in C #
2 answers
You must use the IPAddress class. This bothers you a little because it tries to prevent you from depending on IP4 addresses. The Address element has been deprecated. Here's a workaround:
using System;
using System.Net;
class Program {
static void Main(string[] args) {
var addr = IPAddress.Parse("192.168.0.2");
int ip4 = BitConverter.ToInt32((addr.GetAddressBytes()), 0);
Console.WriteLine("{0:X8}", ip4);
Console.ReadLine();
}
}
Output: 0200A8C0
Note that the address is in correct network order (big end).
+4
a source to share