System.Uri.FromHex C# (CSharp) Method

FromHex() public static method

public static FromHex ( char digit ) : int
digit char
return int
        public static int FromHex(char digit)
        {
            if (((digit >= '0') && (digit <= '9'))
                || ((digit >= 'A') && (digit <= 'F'))
                || ((digit >= 'a') && (digit <= 'f')))
            {
                return (digit <= '9')
                    ? ((int)digit - (int)'0')
                    : (((digit <= 'F')
                    ? ((int)digit - (int)'A')
                    : ((int)digit - (int)'a'))
                    + 10);
            }
            throw new ArgumentException(nameof(digit));
        }

Usage Example

        internal static void Parse(ReadOnlySpan <char> address, Span <ushort> numbers, int start, ref string?scopeId)
        {
            int  number          = 0;
            int  index           = 0;
            int  compressorIndex = -1;
            bool numberIsValid   = true;

            //This used to be a class instance member but have not been used so far
            int PrefixLength = 0;

            if (address[start] == '[')
            {
                ++start;
            }

            for (int i = start; i < address.Length && address[i] != ']';)
            {
                switch (address[i])
                {
                case '%':
                    if (numberIsValid)
                    {
                        numbers[index++] = (ushort)number;
                        numberIsValid    = false;
                    }

                    start = i;
                    for (++i; i < address.Length && address[i] != ']' && address[i] != '/'; ++i)
                    {
                    }
                    scopeId = new string(address.Slice(start, i - start));
                    // ignore prefix if any
                    for (; i < address.Length && address[i] != ']'; ++i)
                    {
                    }
                    break;

                case ':':
                    numbers[index++] = (ushort)number;
                    number           = 0;
                    ++i;
                    if (address[i] == ':')
                    {
                        compressorIndex = index;
                        ++i;
                    }
                    else if ((compressorIndex < 0) && (index < 6))
                    {
                        // no point checking for IPv4 address if we don't
                        // have a compressor or we haven't seen 6 16-bit
                        // numbers yet
                        break;
                    }

                    // check to see if the upcoming number is really an IPv4
                    // address. If it is, convert it to 2 ushort numbers
                    for (int j = i; j < address.Length &&
                         (address[j] != ']') &&
                         (address[j] != ':') &&
                         (address[j] != '%') &&
                         (address[j] != '/') &&
                         (j < i + 4); ++j)
                    {
                        if (address[j] == '.')
                        {
                            // we have an IPv4 address. Find the end of it:
                            // we know that since we have a valid IPv6
                            // address, the only things that will terminate
                            // the IPv4 address are the prefix delimiter '/'
                            // or the end-of-string (which we conveniently
                            // delimited with ']')
                            while (j < address.Length && (address[j] != ']') && (address[j] != '/') && (address[j] != '%'))
                            {
                                ++j;
                            }
                            number           = IPv4AddressHelper.ParseHostNumber(address, i, j);
                            numbers[index++] = (ushort)(number >> 16);
                            numbers[index++] = (ushort)number;
                            i = j;

                            // set this to avoid adding another number to
                            // the array if there's a prefix
                            number        = 0;
                            numberIsValid = false;
                            break;
                        }
                    }
                    break;

                case '/':
                    if (numberIsValid)
                    {
                        numbers[index++] = (ushort)number;
                        numberIsValid    = false;
                    }

                    // since we have a valid IPv6 address string, the prefix
                    // length is the last token in the string
                    for (++i; address[i] != ']'; ++i)
                    {
                        PrefixLength = PrefixLength * 10 + (address[i] - '0');
                    }
                    break;

                default:
                    number = number * 16 + Uri.FromHex(address[i++]);
                    break;
                }
            }

            // add number to the array if its not the prefix length or part of
            // an IPv4 address that's already been handled
            if (numberIsValid)
            {
                numbers[index++] = (ushort)number;
            }

            // if we had a compressor sequence ("::") then we need to expand the
            // numbers array
            if (compressorIndex > 0)
            {
                int toIndex   = NumberOfLabels - 1;
                int fromIndex = index - 1;

                for (int i = index - compressorIndex; i > 0; --i)
                {
                    numbers[toIndex--]   = numbers[fromIndex];
                    numbers[fromIndex--] = 0;
                }
            }
        }
All Usage Examples Of System.Uri::FromHex