C# Unhandled Exception help

Hi guys

I'm making a ceaser shift decryption program for my university assignment and I am completely stuck by this exception.

Unhandled Exception: System.InvalidCastExceprtion; Unable to cast object of type 'System.Char[]' to type 'System.Iconvertible.

 static void Main(string[] args) { string file = @"C:\Users\James\Documents\caesarShiftEncodedText.txt"; string text = File.ReadAllText(file); Console.WriteLine("ContentsofcaesarShiftEncodedText; \n\n{0}", text); //Used to read in the Encrypted Text and Display it on Screen. char[] TextArray = text.ToCharArray(); //Converts the string into single characters. int convertedtext; //Variable to store the text when it is converted from char to int. int shiftedtext; char decryptedtext; char[] alpahbet = new char[26] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; Console.WriteLine("Enter an Interger to shift the code by: "); int shiftvalue = Convert.ToInt32(Console.ReadLine()); foreach (char alphabet in TextArray) ; { convertedtext = Convert.ToInt32(TextArray); shiftedtext = convertedtext + shiftvalue; decryptedtext = Convert.ToChar(shiftedtext); } Console.WriteLine("CeaserShiftDecrypted Text; \n\n{0}", decryptedtext); 

Thanks, Any help would be really appreciated

by jamesg488 via /r/csharp

Leave a Reply