gpt4 book ai didi

c# - WinRT 中的 IPAddress.HostToNetworkOrder() 等价物?

转载 作者:行者123 更新时间:2023-11-30 21:05:17 24 4
gpt4 key购买 nike

IPAddress 类似乎不存在于 WinRT API 中。是否有等效于 IPAddress.HostToNetworkOrder() 的方法可用于将 int 从主机顺序转换为网络顺序(从 Little Endian 到 Big Endian)?

最佳答案

在许多情况下(当然不是所有情况)您可以为此使用DataReaderDataWriter:

DataReader reader = new DataReader(stream.AsInputStream());
reader.ByteOrder = ByteOrder.BigEndian; // standard network byte order
reader.ReadInt16();

出于兼容性原因,我使用了反射器:

    public static short HostToNetworkOrder(short host)
{
return (short)(((host & 0xff) << 8) | ((host >> 8) & 0xff));
}

public static int HostToNetworkOrder(int host)
{
return (((HostToNetworkOrder((short)host) & 0xffff) << 0x10) | (HostToNetworkOrder((short)(host >> 0x10)) & 0xffff));
}

public static long HostToNetworkOrder(long host)
{
return (long)(((HostToNetworkOrder((int)host) & 0xffffffffL) << 0x20) | (HostToNetworkOrder((int)(host >> 0x20)) & 0xffffffffL));
}

public static short NetworkToHostOrder(short network)
{
return HostToNetworkOrder(network);
}

public static int NetworkToHostOrder(int network)
{
return HostToNetworkOrder(network);
}

public static long NetworkToHostOrder(long network)
{
return HostToNetworkOrder(network);
}

关于c# - WinRT 中的 IPAddress.HostToNetworkOrder() 等价物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11798356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com