none
Converting integer to a letter RRS feed

  • Question

  • Hello everyone, I've just started working with C++ moving from C#

     

    in C# i could easily typecast an integer to a character and get a letter and vise versa. 

     

    However when I try to do that in C++ i still get the number back. Here's my code:

     

    if(e->KeyValue::get() != 13 && flag1){	
    				 
    
    
     label1->Text+= (char)e->KeyValue::get();
    
    			
    }
    
    //I've also tried the _itoa_s() function to no avail
    

     

     

    I get the value of the integer using the Form_KeyDown events.

    So when the user presses the "K" key on their keyboard i want the label to add the actual letter "K" to its text. Ive been searching for a solution since yesterday.

    Saturday, December 4, 2010 3:21 PM

Answers

All replies

  • On 04/12/2010 16:21, ppman77 wrote:

    [code]
    if(e->KeyValue::get() != 13&&  flag1){   

      label1->Text+= (char)e->KeyValue::get();

    }

    //I've also tried the _itoa_s() function to no avail

    You may want to try the Unicode version of _itoa_s(), i.e. _itow_s.

    Giovanni

    Saturday, December 4, 2010 3:59 PM
  • Thanks for responding but i still get back a number and not a letter.

    Here's my code:

     

    				 wchar_t buff[10];
    				 _itow_s(e->KeyValue,buff,10,10);
    
    				 String^ s= gcnew String(buff);
    
    				 MessageBox::Show(s);
    

    Saturday, December 4, 2010 4:13 PM
  • Try this too:

     

    label1->Text += Char(e->KeyValue);

    • Marked as answer by jgcoded Saturday, December 4, 2010 4:18 PM
    Saturday, December 4, 2010 4:16 PM
  • Thank you! quick and easy solution!
    Saturday, December 4, 2010 4:18 PM
  • >I still get back a number and not a letter.
    >_itow_s(e->KeyValue,buff,10,10);

    Not too surprising. The functions _itoa(), _itow_s, etc.
    take an integer and convert its value to a string
    representation of that value. If the int holds a value
    of 123 (0x7B) then the resultant char or wchar_t string
    will be == "123" or L"123".

    - Wayne
    Saturday, December 4, 2010 5:49 PM