I was making a fake encryption program and when trying to convert a string into a int32 and i get the following message System.OverflowException. The error message is at the Convert.ToInt32(textnumbers);
static void encryption(string text) { byte[] nums = Encoding.ASCII.GetBytes(text); int[] en = new int[nums.Length]; for (int i = 0; i < nums.Length; i++) { en[i] = nums[i] * 2 + 5; } string textnumbers = string.Join("", en); Console.WriteLine(textnumbers); int num = Convert.ToInt32(textnumbers); string hexValue = num.ToString("x"); Console.WriteLine(hexValue); } static void Main(string[] args) { encryption("abcde"); Console.ReadLine(); }
Console.ReadLine()with a string literal. Also, hopefully this is just a toy/fun project because it's not really encryption.textnumberswhen the exception occurs?textnumberis either over the max value of2147483647or under the min value of-2147483648, my assumption is it's over that max value. You could try usingToInt64and see if that fits...Int32.TryParseinstead. That gives you the advantage of knowing whether it succeeded without throwing an exception.