Search code examples
cinputsdluint

Converting Uint16 to string in C


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


Solution

  • 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);