1

I was trying to learn explicit conversion in c#.

First I explicitly converted string to int

namespace MyfirstCSharpCode
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "1234";
            int i = Convert.ToInt16(str);
            Console.WriteLine(i);

        }
    }
}

it worked and the result is : 1234

Now I tried to convert char to int

namespace MyfirstCSharpCode
{
    class Program
    {
        static void Main(string[] args)
        {
            char str = '1';
            int i = Convert.ToInt16(str);
            Console.WriteLine(i);

        }
    }
}

And now the result is 49, the ASCII value of 1. How to get the char 1 instead of ASCII value. Please don't be too harsh its my first day in c# ;)..

Pரதீப்
  • 91,748
  • 19
  • 131
  • 172
  • 1
    `Console.WriteLine((char)i);` would do it – Alex K. May 27 '17 at 14:41
  • @AlexK. - Thanks bro..Yes it worked. It is like trying to convert the ASCII value back to Character. Why it is converted to `ASCII` value instead of `Int`. – Pரதீப் May 27 '17 at 14:45
  • you hit the overload methods of `Convert.toInt16` , use `Int.tryParse` for int assignment before using `Convert.ToInt16` – Turbot May 27 '17 at 14:45
  • 2
    "Why it is converted to ASCII value instead of Int." It's not clear what you mean. A `char` is a UTF-16 code unit. The integer value of the UTF-16 code unit representing '1' is 49. That's how it's defined. It sounds like you want to look at `char.GetNumericValue`. – Jon Skeet May 27 '17 at 14:45
  • @JonSkeet - When I use `int i = Convert.ToInt16(str);` why it is converted to `ASCII` value `49` instead of `int` value `1` – Pரதீப் May 27 '17 at 14:47
  • 1
    @Prdp: And I've explained that to you. It's giving you the UTF-16 code unit value. It's not trying to parse it as if it were a digit - that's what `char.GetNumericValue` is for. – Jon Skeet May 27 '17 at 14:48
  • @JonSkeet - Big thanks sir.. – Pரதீப் May 27 '17 at 14:50
  • Someone care to write answer though it might not be a new or useful question :/ so that we can close the question – Pரதீப் May 27 '17 at 14:50
  • @Turbot - when I did this `int i = int.tryParse(str);` I got a error saying *int does contain a definition for tryparse*. May be am missing something – Pரதீப் May 27 '17 at 14:55
  • C# is case sensitive. It is `int.TryParse` (it returns a bool and has an out parameter that contains the parsed value). You can also use `int.Parse` if you want the int value immediately, though it would throw an exception if it is an invalid int value. – Styxxy May 27 '17 at 15:13

1 Answers1

2

You hit the overload methods of Conver.ToInt16(char) , and char is a UTF-16 code unit and representing 1 is 49. You can use Int16.TryParse to safe parse the number (need in 16 bit sign number) in order to get the valid integer.

               string str = "1";  //  use string if your number e.g 122
               short number;  // convert to 16 bit signed integer equivalent.
               if (Int16.TryParse(str, out number))
               {  
                   Console.WriteLine(number);

               }
Turbot
  • 5,095
  • 1
  • 22
  • 30