Search code examples
c++charintpdcurses

Output int data in PDcurses


The program I am writing utilizes PDcurses for output, generally the function mvprintw(). I also need to occasionally output a single integer value, however PDcurses does not offer the ability to output integer values.

I've searched a while for good solutions, and cannot find a simple and portable way to convert int data into the required char* type to be used in mvprintw().

What is the generalized method for output of integers when using PDcurses for output? There is only one line of code which outputs an integer, so I'm looking for a solution which adds a little required code/inclusion as possible.


Solution

  • You can use the same sort of format strings for mvprintw as you would for printf, e.g.:

    mvprintw(0, 0, "%d", 42 ); // "42", decimal number output
    mvprintw(1, 0, "%c", '@' ); // "@", character output
    mvprintw(2, 0, "%02x", 42 ); // "2a", hexadecimal number output
    mvprintw(3, 0, "Hello world: %d", 42 ); // "Hello world: 42", some text and a decimal number
    

    The char * that is required is the format string. See the printf documentation for more information on how this should be used.