__os_snprintf code

Discussion in 'Wii U - Hacking & Backup Loaders' started by dimanmonster, Jun 2, 2016.

  1. dimanmonster
    OP

    dimanmonster Newbie

    Newcomer
    3
    0
    May 19, 2016
    Serbia, Republic of
    Hello! Please give an example of the code that displays the value of the variable on the screen __os_snprintf
     
  2. QuarkTheAwesome

    QuarkTheAwesome Working for Hugs

    Member
    809
    1,992
    Apr 19, 2015
    Australia
    Stuck in the PowerPC
    __os_snprintf doesn't display a variable on the screen, it simply allows you to create a string with the variable in it which you can then print using another method.

    Code:
    char buffer[255];
    __os_snprintf(buffer, 255, "format string", variable1, variable2, etc);
    somehow_print_string(buffer);
    
    "format string" is the same kind of format string that most C functions use. An example of this is could be "number is %d, letter is %c, hex is %X, string is %s". If I put in the data 1, g, 0x4F and "apple", this would create the string "number is 1, letter is g, hex is 4F, string is apple", ready for printing, but I cannot stress enough that it does not actually print. You'll need to do this another way (OSScreen for instance).
    You can find a full list of things you can put in a format string here.
     
  3. brienj

    brienj Trying to avoid getting cancer

    Member
    1,232
    1,705
    Jan 3, 2016
    United States
    It's pretty much exactly the same thing as snprintf, and in fact, you can use snprintf instead, as long as you include <stdio.h> in your project.
     
  4. BullyWiiPlaza

    BullyWiiPlaza Nintendo Hacking <3

    Member
    1,749
    1,423
    Aug 2, 2014
    Germany
    This is an old topic but I'll post a reply anyway. Here is a full example code:
    Code:
    #define SERVER_VERSION "01/14/2017"
    #define ERROR_BUFFER_SIZE 100
    #define COMMAND_BYTE 0xFF
    void reportIllegalCommandByte() {
        char errorBuffer[ERROR_BUFFER_SIZE];
        __os_snprintf(errorBuffer, ERROR_BUFFER_SIZE, "Illegal command byte received: %i\nServer Version: %s", COMMAND_BYTE, SERVER_VERSION);
        OSFatal(errorBuffer);
    }
    This can be used when the TCP Gecko Installer receives an unknown command byte for example. It will format the string by inserting the command byte and the server version. It works the same as the standard snprintf.

    Result:
    Code:
    Illegal command byte received: 255
    Server Version: 01/14/2017