0

i'm trying to use SDL and read user input, for that I need to convert Uint16 to string, here is my code:

      if( event.type == SDL_KEYDOWN )
                {

                    if( strlen(str) <= 16 )
                    {
                        if( ( event.key.keysym.unicode >= (Uint16)'a' ) && ( event.key.keysym.unicode <= (Uint16)'z' ) )
                        {
                            //imprimir("espaco");
                            //strcat(str, (char*)event.key.keysym.unicode);
                            imprimir((char*)event.key.keysym.unicode);
                        }
}
}

i can't make it work, I already tried itoa, atoi, strcat, summing event.key.keysym.unicode to a int then converting to char, i'm new to C, thanks

  • Start with a good book perhaps. There are no native strings in C, only arrays of characters. The issue isn't entirely trivial, and if you're not comfortable with that idea, you'll keep running into problems. – Kerrek SB Oct 31 '11 at 14:32

2 Answers2

1

Do you want to have the key-code (the number)?

char buffer[16];
snprintf(buffer, sizeof(buffer), "%d", event.key.keysym.unicode);
imprimir(buffer);

Or do you want the representing character?

char buffer[2];
snprintf(buffer, sizeof(buffer), "%c", event.key.keysym.unicode);
imprimir(buffer);
Constantinius
  • 34,183
  • 8
  • 77
  • 85
0

Have you tried sprintf? http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/

Nanocom
  • 3,696
  • 4
  • 31
  • 46