Ejemplo n.º 1
0
        public static unsafe bool Ipv4StringToAddress(ReadOnlySpan <char> ipSpan, out long address)
        {
            int  end = ipSpan.Length;
            long tmpAddr;

            fixed(char *ipStringPtr = &MemoryMarshal.GetReference(ipSpan))
            {
                tmpAddr = IPv4AddressHelper.ParseNonCanonical(ipStringPtr, 0, ref end, notImplicitFile: true);
            }

            if (tmpAddr != IPv4AddressHelper.Invalid && end == ipSpan.Length)
            {
                // IPv4AddressHelper.ParseNonCanonical returns the bytes in the inverse order.
                // Reverse them and return success.
                address = BinaryPrimitives.ReverseEndianness((uint)tmpAddr);
                return(true);
            }
            else
            {
                // Failed to parse the address.
                address = 0;
                return(false);
            }
        }
Ejemplo n.º 2
0
 public static short HostToNetworkOrder(short host)
 {
     return(BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(host) : host);
 }