Microsoft.Protocols.TestSuites.Common.Common.GetBytesFromBinaryHexString C# (CSharp) Method

GetBytesFromBinaryHexString() public static method

Convert a hex16 string to byte array
public static GetBytesFromBinaryHexString ( string str ) : byte[]
str string The original hex16 string
return byte[]
        public static byte[] GetBytesFromBinaryHexString(string str)
        {
            char[] charArray = str.ToLower().ToCharArray();
            if (str.Length % 2 != 0)
            {
                throw new Exception("The hex16 string is invalid");
            }

            byte[] byteArray = new byte[str.Length / 2];
            int index = 0;
            int tempHex = 0;
            for (int i = 0; i < charArray.Length;)
            {
                char a = charArray[i++];

                if (a >= 'a' && a <= 'f')
                {
                    switch (a)
                    {
                        case 'a':
                            tempHex = 10;
                            break;
                        case 'b':
                            tempHex = 11;
                            break;
                        case 'c':
                            tempHex = 12;
                            break;
                        case 'd':
                            tempHex = 13;
                            break;
                        case 'e':
                            tempHex = 14;
                            break;
                        case 'f':
                            tempHex = 15;
                            break;
                    }
                }
                else if (a >= '0' && a <= '9')
                {
                    tempHex = Convert.ToInt32(a) - 48;
                }
                else
                {
                    throw new Exception("The hex16 string is invalid");
                }

                a = charArray[i++];
                if (a >= 'a' && a <= 'f')
                {
                    switch (a)
                    {
                        case 'a':
                            tempHex = (tempHex * 16) + 10;
                            break;
                        case 'b':
                            tempHex = (tempHex * 16) + 11;
                            break;
                        case 'c':
                            tempHex = (tempHex * 16) + 12;
                            break;
                        case 'd':
                            tempHex = (tempHex * 16) + 13;
                            break;
                        case 'e':
                            tempHex = (tempHex * 16) + 14;
                            break;
                        case 'f':
                            tempHex = (tempHex * 16) + 15;
                            break;
                    }
                }
                else if (a >= '0' && a <= '9')
                {
                    tempHex = (tempHex * 16) + Convert.ToInt32(a) - 48;
                }
                else
                {
                    throw new Exception("The hex16 string is invalid");
                }

                byteArray[index++] = Convert.ToByte(tempHex);
            }

            return byteArray;
        }
Common