1

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();

    }
  • Does this answer your question?[How can I convert String to Int?](https://stackoverflow.com/questions/1019793/how-can-i-convert-string-to-int) – Soheila Tarighi Apr 06 '20 at 15:34
  • We don't know **what you typed**. It would be more helpful to replace `Console.ReadLine()` with a string literal. Also, hopefully this is just a toy/fun project because it's not really encryption. – Damien_The_Unbeliever Apr 06 '20 at 15:35
  • What is the value of ``textnumbers`` when the exception occurs? – user1666620 Apr 06 '20 at 15:35
  • 4
    The actual error is because `textnumber` is either over the max value of `2147483647` or under the min value of `-2147483648`, my assumption is it's over that max value. You could try using `ToInt64` and see if that fits... – Trevor Apr 06 '20 at 15:38
  • 2
    Consider using [```Int32.TryParse```](https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=netframework-4.8) instead. That gives you the advantage of knowing whether it succeeded without throwing an exception. – devsmn Apr 06 '20 at 15:47
  • @Shawn in this case I'm glad the user didn't use `TryParse`, so they can see their mistake and understand why. I agree though, `TryParse` would be great though for the user to be able to handle such situations. – Trevor Apr 06 '20 at 15:49
  • @user1666620 the value is abcde – Lucas Hirtzbruch Apr 06 '20 at 15:53
  • The full error message says exactly what the problem is. `Value was either too large or too small for an Int32.` You're trying to turn `199201203205207` into a 32 bit integer, which is way outside of the range for a 32 bit int. – TheBatman Apr 06 '20 at 16:04

2 Answers2

1

The important part is to pay attention to the exception message that the System.OverflowException produces, which is:

Value was either too large or too small for an Int32.

That gives you a big clue as to what the problem is.

Next, if you take a look at the value of textnumbers after the for loop has been completed, you'll see its value is 199201203205207. As Çöđěxěŕ mentioned, the maximum value a signed 32-bit integer can store is 2147483647, so the exception is telling you a 32-bit integer isn't large enough to store the value you want.

To fix this, you can use a 64-bit integer:

long num = Convert.ToInt64(textnumbers);

long represents a signed 64-bit integer, and can store values in the range -9223372036854775808 to 9223372036854775807, inclusive, which can comfortably store 199201203205207.

If ever you need to know the range of values a numeric type can represent, you can examine its MinValue and MaxValue properties (e.g. int.MinValue, or long.MaxValue).

One thing though: you should learn to work with the debugger, because it makes fixing this kind of problem by yourself trivial. It's worth investing that time so you can self-service problems like this, without relying on others.

John H
  • 14,422
  • 4
  • 41
  • 74
0

Your program generates '199201203205207' which is greater than what you could fit in int. try changing it to long ,also use Convert.ToInt64(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);

    long num = Convert.ToInt64(textnumbers);

    string hexValue = num.ToString("x");
    Console.WriteLine(hexValue);

}

static void Main(string[] args)
{

    encryption("abcde");
    Console.ReadLine();

}
Sina Hoseinkhani
  • 318
  • 4
  • 15