什么是pcl兼容的IPAddress等价物?NetworkToHostOrder方法
本文关键字:等价物 NetworkToHostOrder 方法 IPAddress pcl 什么 | 更新日期: 2023-09-27 18:02:56
System.Net.IPAddress
类不适用于。net中的PCL(可移植类库)。我试图将一些处理二进制流的代码迁移到PCL,并且广泛使用IPAddress
的NetworkToHostOrder
方法(有三个过载)。什么是等效的pcl兼容代码?
最终借用了Mono:
internal static class EndianConverter
{
public static long NetworkToHostOrder(long network)
{
return !BitConverter.IsLittleEndian ? network : SwapLong(network);
}
public static int NetworkToHostOrder(int network)
{
return !BitConverter.IsLittleEndian ? network : SwapInt(network);
}
public static short NetworkToHostOrder(short network)
{
return !BitConverter.IsLittleEndian ? network : SwapShort(network);
}
public static short HostToNetworkOrder(short host)
{
return !BitConverter.IsLittleEndian ? (host) : SwapShort(host);
}
public static int HostToNetworkOrder(int host)
{
return !BitConverter.IsLittleEndian ? (host) : SwapInt(host);
}
public static long HostToNetworkOrder(long host)
{
return !BitConverter.IsLittleEndian ? (host) : SwapLong(host);
}
private static short SwapShort(short number)
{
return (short) (((number >> 8) & 0xFF) | ((number << 8) & 0xFF00));
}
private static int SwapInt(int number)
{
return (((number >> 24) & 0xFF)
| ((number >> 08) & 0xFF00)
| ((number << 08) & 0xFF0000)
| ((number << 24)));
}
private static long SwapLong(long number)
{
return (((number >> 56) & 0xFF)
| ((number >> 40) & 0xFF00)
| ((number >> 24) & 0xFF0000)
| ((number >> 08) & 0xFF000000)
| ((number << 08) & 0xFF00000000)
| ((number << 24) & 0xFF0000000000)
| ((number << 40) & 0xFF000000000000)
| ((number << 56)));
}