Sorry to ask about what should be a simple thing, but I've been trying to use IPAddress.NetworkToHostOrder
and IPAddress.HostToNetworkOrder
to no avail. I'm on Windows rig, little endian. Here's tiny code for what's puzzling me:
using System;
using System.Net;
public class Program
{
public static void Main()
{
int addr = 1;
Console.WriteLine($"{IPAddress.NetworkToHostOrder(addr)}");
Console.WriteLine($"{IPAddress.HostToNetworkOrder(addr)}");
}
}
The output for this is:
16777216
16777216
The output for one of these should be 1
, right? I've given up, and wrote a replacement to handle the byte sequencing issue, but it's driving me nuts that I haven't been able to use IPaddress
for this.
Those functions assume that you've provided an input value in a given byte order and always want to swap the byte order to the opposite byte order. They have no way of knowing the byte order of the value you've provided. So, they both always swap the order of what's given to them.
As indicated in this answer, one reason that both functions exist is to serve as documentation and indicate which order (host vs network) you're going from/to.