none
convert from unicode to integer RRS feed

  • Question

  • Hi all,

    How can I convert a unicode value to its relevant integer value and print it.
    For Example:
    When I enter 49(unicode), it's equivalent integer value 1 should be printed.
    How can I achieve this?
    Please help!!

    Thanks in advace..
    Monday, October 21, 2013 6:12 PM

Answers

  • By "equivalent integer value" do you mean that 49 decimal relates to the character '1'?  If so, all you need to do (if you only need ASCII characters) is type-cast the int to a char.  Example:

    class Program
      {
        static void Main(string[] args)
        {
          while (true)
          {
            Console.WriteLine("Enter a number between 0-255: ");
            string line = Console.ReadLine();
            int index = line.IndexOf(' ');
            byte number = Convert.ToByte(line.Substring(0, (index == -1) ? line.Length : index));
            Console.WriteLine("{0} (dec) = {1} (char)", number, (char)number);
          }
        }
      }


    • Marked as answer by Eason_H Monday, October 28, 2013 1:30 AM
    Monday, October 21, 2013 6:25 PM
  • Hi,

    According your description. I think you can use the Convert.ToInt32 Method. It converts the value of the specified Unicode character to the equivalent 32-bit signed integer. A 32-bit signed integer that represents the UTF-16 encoded code point of the value parameter.

    char[] chars = { 'a', 'z', '\u0007', '\u03FF',
                     '\u7FFF', '\uFFFE' };
    int result;
    
    foreach (char ch in chars)
    {
       try {
          result = Convert.ToInt32(ch);
          Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.",
                            ch.GetType().Name, ch,
                            result.GetType().Name, result);
       }
       catch (OverflowException) {
          Console.WriteLine("Unable to convert u+{0} to an Int32.",
                            ((int)ch).ToString("X4"));
       }
    }   
    

    The example displays the following output: 
    Converted the Char value 'a' to the Int32 value 97. 
    Converted the Char value 'z' to the Int32 value 122. 
    Converted the Char value '' to the Int32 value 7. 
    Converted the Char value '?' to the Int32 value 1023. 
    Converted the Char value '?' to the Int32 value 32767. 
    Converted the Char value '?' to the Int32 value 65534.

    Thanks.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Eason_H Monday, October 28, 2013 1:29 AM
    Tuesday, October 22, 2013 1:54 AM

All replies

  • By "equivalent integer value" do you mean that 49 decimal relates to the character '1'?  If so, all you need to do (if you only need ASCII characters) is type-cast the int to a char.  Example:

    class Program
      {
        static void Main(string[] args)
        {
          while (true)
          {
            Console.WriteLine("Enter a number between 0-255: ");
            string line = Console.ReadLine();
            int index = line.IndexOf(' ');
            byte number = Convert.ToByte(line.Substring(0, (index == -1) ? line.Length : index));
            Console.WriteLine("{0} (dec) = {1} (char)", number, (char)number);
          }
        }
      }


    • Marked as answer by Eason_H Monday, October 28, 2013 1:30 AM
    Monday, October 21, 2013 6:25 PM
  • Hi,

    According your description. I think you can use the Convert.ToInt32 Method. It converts the value of the specified Unicode character to the equivalent 32-bit signed integer. A 32-bit signed integer that represents the UTF-16 encoded code point of the value parameter.

    char[] chars = { 'a', 'z', '\u0007', '\u03FF',
                     '\u7FFF', '\uFFFE' };
    int result;
    
    foreach (char ch in chars)
    {
       try {
          result = Convert.ToInt32(ch);
          Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.",
                            ch.GetType().Name, ch,
                            result.GetType().Name, result);
       }
       catch (OverflowException) {
          Console.WriteLine("Unable to convert u+{0} to an Int32.",
                            ((int)ch).ToString("X4"));
       }
    }   
    

    The example displays the following output: 
    Converted the Char value 'a' to the Int32 value 97. 
    Converted the Char value 'z' to the Int32 value 122. 
    Converted the Char value '' to the Int32 value 7. 
    Converted the Char value '?' to the Int32 value 1023. 
    Converted the Char value '?' to the Int32 value 32767. 
    Converted the Char value '?' to the Int32 value 65534.

    Thanks.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Eason_H Monday, October 28, 2013 1:29 AM
    Tuesday, October 22, 2013 1:54 AM
  • Take the string input, parse it to an int and cast that int to a char.

    Console.WriteLine((char)int.Parse(Console.ReadLine()));

    You should add error handling in case someone types 'Hello' or something which is not an int.


    Paul Linton

    Tuesday, October 22, 2013 2:19 AM