Search code examples
c#variablesintegerdouble

Variable changes absurdly like 3 to 51?


I have been trying to solve a codewar question but when I use char to int .I get absurd results.

like I enter 3 as base and 3 as power but result

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
class Program
{
    static void Main(string[] args)
    {
    ...

    int cL;
    char[] charArray = Sayı.ToCharArray();

    foreach(char chr in charArray)  // getting digits multiply with himself for L times
    {                             
        cL = chr;                 //    chr İS 3 BUT WHEN ASSİGNED TO cL İT CHANGES TO 51
        Console.WriteLine(chr)    // gives 3
        Console.WriteLine(cL)     // gives 51
        double nmbr = Math.Pow(cL, L);
        ...
    }

Solution

  • Characters have codes, your character 3 is a value of 51. See here for list of all characters:

    https://www.w3schools.com/charsets/ref_html_ascii.asp