Ask a questionAsk a question
 

AnswerEquivalent ASCII code value for key pressed

  • Friday, October 30, 2009 11:26 AMPrabu.P Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    In .Net framework 3.5 (WPF application), when I try to cast entered key into byte or int, I am getting wrong ASCII value. If I press A, I am getting 44 as casted value but it should be 65. Please let me know if am I doing anything wrong?


    Prabu
    • Moved byYiChun ChenMSFTMonday, November 02, 2009 10:18 AMWPF issue (From:.NET Framework Setup)
    •  

Answers

All Replies

  • Saturday, October 31, 2009 8:51 AManlis Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi

    Plz post the code sample .....

    Refer this examples
    Does this help

    The Visual Basic Title Convert a character to and from its ASCII code
    Keywords ASCII, value, character
    Categories Utilities
     
     
     
    Use Asc to get a character's ASCII code. Use Chr to convert an ASCII code into a character.
    Thanks to Adan Rivas
     
     
     
    Private Sub Command2_Click()
         'Code the character
         Label3 = Asc(Text1)
         'clears text box
         Text1 = ""
         ' adds the coded character to textbox (label3)
         LABEL6 = LABEL6 + "," + Label3
         ' set focus to textbox
         Text1.SetFocus
         'decodes the coded character
         Text2 = Text2 + Chr(Label3)
    End Sub

    Private Sub Command3_Click()
         'this part decodes the coded number
         Label3 = Text3
         Text3 = ""
         LABEL6 = LABEL6 + "," + Label3
         Text2 = Text2 + Chr(Label3)
         Text3.SetFocus
    End Sub
     
    Chr function takes an ASCII character code and returns the character.

    Print Chr(65)   ' prints "A"


    Thks
    anliS


    Best Regards,
    anliS
    BIC
    Please remember to mark the correct replies as answers

  • Monday, November 02, 2009 5:53 AMPrabu.P Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Did you run your code in .Net framework 3.5 (WPF application), when I try to cast entered key into either byte or int , I am getting wrong ASCII value. If I press A, I am getting 44 as casted value but it should be 65 . Please let me know if am I doing anything wrong?

    This is the code I am using in my application.

    private void TextBox_KeyDown(object sender, KeyEventArgs e)
    {            
          MessageBox.Show(string.Format("Key : {0} \nKeyCode : {1}", e.Key, (byte)e.Key));
    }
    

    Prabu
  • Monday, November 02, 2009 10:17 AMYiChun ChenMSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Prabu,

    I am moving this thread from Base ".NET Framework Setup" forum to the "Windows Presentation Foundation (WPF)" forum, since the issue is related to WPF. There are more WPF experts in the "Windows Presentation Foundation (WPF)" forum.

    Thanks
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Tuesday, November 03, 2009 10:51 AMJim Zhou - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Prabu.P,

    You can first get the Char type from the string, and then use Convert.ToInt32 method to do the converter as shown below.
    Code snippet:

    string s =e.Key.ToString();
    char[] chars = s.ToCharArray();
    char chr = chars[0];
    Console.WriteLine (System.Convert.ToInt32(chr));

    Thanks.
    Sincerely.


    Jim Zhou -MSFT
  • Wednesday, November 04, 2009 10:48 AMPrabu.P Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks for the code snippet. But this makes no difference between Upper and lowercase. How do I differenciate this?

    The ASCII value of the charactor should be common for all launguage rite? Why does it was changes in .Net 3.5 framework.? That is my actual question.

    Thanks for your time and reply.


    Prabu
  • Thursday, November 05, 2009 9:29 AMJim Zhou - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Prabu.P,
    -->But this makes no difference between Upper and lowercase. How do I differenciate this?
    It is not because there is no difference between the uppercase and the lowercase, when you press keys on keyboard, the e.Key value will always be the uppercase,  so you just see the ASCII is the same. For the sample below, you can see that for "a", the ASCII is 97, and for "A",  the ASCII is 65.
    Code snippet:

    string s = "a";

    char[] chars = s.ToCharArray();

    char chr = chars[0];

    Console.WriteLine(System.Convert.ToInt32(chr)); //output 97

     

    string s1 = "A";

    char[] chars1 = s1.ToCharArray();

    char chr1 = chars1[0];

    Console.WriteLine(System.Convert.ToInt32(chr1)); //output 65


    -->The ASCII value of the charactor should be common for all launguage rite? Why does it was changes in .Net 3.5 framework.?

    You are right,  the ASCII values are the same for all kinds of languages, actually, I just ran the code above on my machine which has .Net 3.5  Framework install.

    Thanks.
    Sincerely.


    Jim Zhou -MSFT
  • Thursday, November 05, 2009 12:24 PMPrabu.P Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello,

    The code you were given is rite. My actual question is, how do I get the actual key's ASCII value and how to differentiate it. I should get 65 when I press "A" and "97" when I press "a". I want the code snippet for this.

    Hope you should clear now.

    Thanks for your time.
    Prabu
  • Friday, November 06, 2009 2:21 PMJim Zhou - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Prabu.P,

    I got it, in this case, one workaround is that you can check the status of Key.Capital, since it's a little late in our time zone and now I afraid I have to go home, I will continually see to this case and let you know the result if I get a better idea.
    Have a nice weekend.

    Thanks.
    Sincerely.
    Jim Zhou -MSFT
  • Monday, November 16, 2009 1:12 PMPrabu.P Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    I hope this is not an expected resultant for my query. Could you please post the workaround or solution for this query?

    Thanks for your time.
    Prabu
  • Tuesday, November 17, 2009 8:48 AMJim Zhou - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Prabu.P,

    Sorry for the late reply. 
    KeyDown events correspond to WM_KEYDOWN messages, which are raw key input—roughly corresponding to physical key presses. Character/text input occurs after translation of raw key events, which can be quite non-trivial in the case of IMEs. You should handle PreviewTextInput events instead, and the utilize the e.Text property to get the character you are just pressing.

    Here’s some good background reading:

      http://msdn.microsoft.com/en-us/library/ms646267(VS.85).aspx

      http://msdn.microsoft.com/en-us/library/ms754010.aspx#text_inputReal

    Thanks.
    Sincerely.


    Jim Zhou -MSFT
  • Tuesday, November 24, 2009 6:45 AMJim Zhou - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Prabu.P,

    If you are still having any issues with this, please feel free to ask.

    Thanks.
    Sincerely.


    Jim Zhou -MSFT