15,964,117 members
See more:
C#
```string n = Console.ReadLine();
string m = "";
for (int i = n.Length - 1; i >= 0; i--)
{
m = m + n[i];
}
Console.WriteLine("Added number in reverse order: " + m + "\n");
Console.WriteLine("The result should have been so if the n = 103(i.e reverse view 301):");
Console.WriteLine("1 * (8^0) = " + (1 * Math.Pow(8, 0)));
Console.WriteLine("0 * (8^1) = " + (0 * Math.Pow(8, 1)));
Console.WriteLine("3 * (8^2) = " + (3 * Math.Pow(8, 2)));
Console.WriteLine("Summ = 1 + 0 + 192 = 193 - correct result");
double s = 0, s1 = 0;
Console.WriteLine("\nStarting cycle:\n");
for (int i = 0; i < m.Length; i++)
{
Console.Write(i + " cycle = ");
s1 = Convert.ToInt64(m[i]) * Math.Pow(8, i);
Console.WriteLine(s1);
s = s + (Convert.ToInt32(m[i]) * Math.Pow(8, i));
}
Console.WriteLine("Summ = " + s +" - uncorrect result");

What I have tried:

Maybe there is a problem when converting variable?. Help me, pleace!
Posted
Updated 11-Nov-16 3:23am
Philippe Mori 5-Nov-16 11:14am
Uses a debugger, It would be very easy to figure out the problem.
You should learn that a string 301 is not equal to integer 301.

## Solution 2

In C/C++C#, the technique to convert `'3'` to `3` is not to use a `convert` function, it is to use `'3'-'0'`.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

v2

## Solution 1

You are using `Convert.ToInt(char)`. This will return the value of the character (e.g. the character '1' has the value 0x31 / 49).
See Convert.ToInt32 Method (Char) (System)[^]:
Quote:
The ToInt32(Char) method returns a 32-bit signed integer that represents the UTF-16 encoded code unit of the value argument. If value is not a low surrogate or a high surrogate, this return value also represents the Unicode code point of value.

But you want the number represented by the character. If you know that the character is a digit, you can get the number by subtracting '0':
C#
`s1 = Convert.ToInt64(m[i] - '0') * Math.Pow(8, i);`

## Solution 3

Use the following piece of code.
m[i] returns a char. So when you convert it into integer it actually returns the ASCII value of the number. so '1' becomes 49.
Just convert it to string and things should work fine.

C#
```for (int i = 0; i < m.Length; i++)
{
Console.Write(i + " cycle = ");
s1 = Convert.ToInt64(m[i].ToString()) * Math.Pow(8, i);
Console.WriteLine(s1);
s = s + (Convert.ToInt32(m[i].ToString()) * Math.Pow(8, i));
}```

Note: m is a string but m[i] is eg: m[0]= '1' is a char.

v2