0
int num = Convert.ToInt32(Console.ReadLine());

With above code I am getting the correct integer value and I am able to do operation on this number and getting the correct result.

But with this below code why Convert.ToInt32(o) method is not converting it to integer value. Why we need to minus with 48.

int[] numarr = number.ToString().Select(o => Convert.ToInt32(o)-48).ToArray();

If I am not subtracting with 48 I am not getting the correct integer value.

Please can anyone explain why this is? Is it required to do every time? Because somewhere else I calculated result without subtracting 48 and I got a correct result.

I am doing a program to print number of occurrences in a number. Here is my code:

Console.WriteLine("Enter the number:");
int number = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter the number to search:");
int searchnumber = Convert.ToInt32(Console.ReadLine());
int cnt = 0;

int[] numarr = number.ToString().Select(o => Convert.ToInt32(o)-48).ToArray();
for (int i = 0; i < numarr.Length; i++)
{
    Console.WriteLine(numarr[i]);
}
for (int i = 0; i < numarr.Length; i++)
{
    if (numarr[i] == searchnumber)
    {
        cnt++;
    }
}

Console.WriteLine("Number of occurence of given number is:{0}", cnt);
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Surabhi Pandey
  • 4,058
  • 4
  • 18
  • 25

1 Answers1

7

Because you are converting the number to a string, the integers you retrieve are actually Unicode code points of the characters in the string. According to the ASCII code table, the 0 character starts at position 48.

So you actually found a workaround to convert characters to their integer representation. If you just want to get the number in a less hacky way, you could use this answer, which uses the modulus operator. Or char.ToNumericValue on your original string, as Time Schmelter proposed in a comment.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 1
    An even less hacky way would be to use [`char.GetNBumericValue`](https://msdn.microsoft.com/en-us/library/e7k33ktz(v=vs.110).aspx): `int[] numarr = number.ToString().Select(c => (int)char.GetNumericValue(c)).ToArray();` – Tim Schmelter Jan 02 '17 at 10:23
  • @TimSchmelter converting to a string to convert back to numbers again feels bad. – Patrick Hofman Jan 02 '17 at 10:27
  • [Int to BCD](http://stackoverflow.com/questions/2448303/converting-a-int-to-a-bcd-byte-array) would be better and faster, even though it isn't a single line – Panagiotis Kanavos Jan 02 '17 at 10:30
  • @patrick: well, OP has already converted the string to `int`. He just wants to get an array of every single char. If he doesn't need the original integer he doesn't need to convert it. – Tim Schmelter Jan 02 '17 at 10:31
  • Yeah, agree. @Tim. Updated. – Patrick Hofman Jan 02 '17 at 10:31
  • @TimSchmelter: Thank you for giving another way of doing it. – Surabhi Pandey Jan 02 '17 at 10:38
  • @PatrickHofman: Thanks for the explanation. Somewhere I knew that this would be the reason. But in other program I convert int in int[] array without doing -48 and got correct result. So I got confused. Do we need to do this always? – Surabhi Pandey Jan 02 '17 at 10:41
  • 2
    @SurabhiPandey: only if you want to convert a `char` to `int`. If it's a string you dont need it – Tim Schmelter Jan 02 '17 at 11:04
  • @TimSchmelter: Can You explain this concept why with char and why not with string. Please post in an answer tab and not in comment So that I can accept your answer and so every one will come to know the difference and the concept behind this. – Surabhi Pandey Jan 02 '17 at 11:07
  • 1
    Characters on their own are something different than a string converted to an integer. Characters are actually integers in disguise, pointing to the place in the code table rather than the 'actual' integer value. – Patrick Hofman Jan 02 '17 at 11:09