Midhila Mohandas

Midhila Mohandas

  • NA
  • 13
  • 1.4k

HexadecimalStringToByteArray() throw exception in C#

Dec 21 2021 5:58 AM

I am practicing HexadecimalStringToByteArray() on VS2017. But throw exception. Could you please tell me more information about it?

Exception alert **An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: Could not find any recognizable digits.**

public static byte[] HexadecimalStringToByteArray(string input)
        {
            var outputLength = input.Length / 2;
            var output = new byte[outputLength];
            using (var sr = new StringReader(input))
            {
                for (var i = 0; i < outputLength; i++)
                    output[i] = Convert.ToByte(new string(new char[2] { (char)sr.Read(), (char)sr.Read() }), 16);
            }
            return output;
}

 


Answers (2)