0

My problem is with this simple code.. When my input is lets say "6 6 2", and i try to convert to Int32, the result is something else than i expected.. Here it is: 6 6 2 54

Here is the code of my simple program in C#:

using System;

public class Program
{
    public static void Main()
    {
        string i = Console.ReadLine();
        int a = Convert.ToInt32(i[0]);
        int b = Convert.ToInt32(i[2]);
        int c = Convert.ToInt32(i[4]);
        Console.WriteLine(a);
    }
}

Please help, I am stuck with this

Vikousek
  • 9
  • 2

3 Answers3

0

You get the ascii code of the char '6'. So 54.

GibbOne
  • 629
  • 6
  • 10
  • 1
    Thank you, that helped me alot.. I was wondering why is it printing 54, here is the answer. Thanks – Vikousek Dec 04 '21 at 21:14
-2

You need to do this first.

string[] ints = line.Split(" ");
int x0 = Convert.ToInt32(ints[0]);
...

Or even more C#'ishly.

int[] ints = line
  .Split(" ")
  .Select(a => Convert.ToInt32(a))
  .ToArray();

You're approaching it the right way but the input 2 5 62 7 isn't four integers to the computer. It's just a string. You need to split it into an array (using e.g. space as a divider). Then, you can convert the individual pieces.

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • This doesn't answer the question why it returns 54 instead of 6. And "2" is still a string. (Int)("662"[0]) also yields 54 – Chrᴉz remembers Monica Dec 04 '21 at 20:44
  • @ChrᴉzremembersMonica You're correct. If you take a look at the OP's background and follow his reasoning, you'll surely realize that it's not the question he mean to ask. And if you're familiar with how stuff's done in Python/JS, you'll notice what he's trying to accomplish. So I answered to what he needs to know not what he's actually asking. I doubt he can ask the proper question due to confusion. – Konrad Viltersten Dec 04 '21 at 20:47
  • @derpirscher Admittedly, I may be mistaken here but I assumed that the poor dude doesn't even realizes that the split to an array hasn't taken place the way he hopes. So I assumed a need which is not stated. – Konrad Viltersten Dec 04 '21 at 20:49
  • You don't need to split if you want to convert single chars ... He is accessing the correct indexes, but had a misunderstanding what the `char` type is. Something that's explained perfectly in the referenced question ... – derpirscher Dec 04 '21 at 20:52
  • 1
    On a second glance i just noticed, your code wouldn't even compile. Letting the typo `Spilt` aside, `ints[0]` is a string, which can't be assigned to `int x` – derpirscher Dec 04 '21 at 20:57
  • @derpirscher I still find it strange to see a string in C# as an array of chars (although it definitely is, I sense that the question asked may not be the question needed. But I may be mistaken and, regardless, the question is asked as is. Also, yes, typo in my answer. Not relevant to the assumption I'm making, though. Thanks for the comment. – Konrad Viltersten Dec 05 '21 at 11:43
  • @ChrᴉzremembersMonica Thanks for the comment. I sense certain hostility in it so I'll assume that expressed myself unsensitively and I'll start off with an apology. My assessment of the OP's background is not based on what you seem to think, though, but may very well be incorrect. And when it comes to my background with C#, I an safely assure you, it's sufficient. I'd explain more but this is not the right forum for objurgation. Let's keep it dignified - after all, we all try to help each other, don't we? – Konrad Viltersten Dec 05 '21 at 11:56
  • No offense, but if you have a "sufficent" background in C#, how comes you try to assign strings to int variables? – derpirscher Dec 05 '21 at 12:06
  • @derpirscher That's an excellent point. That's pure sloppiness on my hand. I was so focused on "spilting" part (typo intended as a funny reference to your correction earlier) that I forgot to add the "convert"-part from the question. This was probably one of my lousiest contributions ever. That what happens when tired and not paying attention. Boy, am I glad I haven't mentioned how many years of C# I actually have in my backpack. That would make it embarrassing, hahaha. I guess getting senile happens to everybody sooner or later. – Konrad Viltersten Dec 05 '21 at 12:17
  • @derpirscher Oh, absolutely no offense taken. You asked politely and specifically. And you're right in your observation, too. Just wanted that to be clearly stated. I always admit my mistakes. Part of learning. :) – Konrad Viltersten Dec 05 '21 at 12:19
  • @konradviltersten ok, seems I interpreted your comment to harsh. My apologies. Have a good time – Chrᴉz remembers Monica Dec 05 '21 at 20:04
  • @ChrᴉzremembersMonica No problems, mate. It may very well me being unintentionally obnoxious. Or maybe something with culture being lost in translation. I'm certain you had all the best intentions and I consider it as a learning interaction. (Also, I seriously ducked up my answer due to sloppiness, so I do deserve a smack in the back of my head.) Happy coding Christmas. :) – Konrad Viltersten Dec 06 '21 at 19:24
-3

If you type in "6 6 2" - that is ONE LINE.

You read it into a string (i).

i[0] is the first character. So far so good. Sadly, Convert.ToInt32 will not give you the NUMERIC value. It will give you the unicode value oft the 6, in your case. Those are not identical.

If you look it up at https://en.wikipedia.org/wiki/List_of_Unicode_characters

you see that Digit Six has a value of 36 - that is iirc identical with the old ASCII table. The first characters are special cases (carriage return, line feed).

Your best bets are:

  • Deduct the value of Decimal 0 (6 comes 6 after that one).

OR - and better:

  • Split the String (String.Split) along the space.
  • Then PARSE the individual substrings into strings. Parsing will result in a proper handling, also for larger numbers (like 22). For that use - the Parse method on the Int32 class.
TomTom
  • 61,059
  • 10
  • 88
  • 148