Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

printf();

Can someone explane to me what that means :
[code]sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40) [/code]

I interesting most for :
"%c[%d;%d;%dm" ?
0x1B ?

Comments

  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : Can someone explane to me what that means :
    : [code]: sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40) [/code]:
    :
    : I interesting most for :
    : "%c[%d;%d;%dm" ?
    : 0x1B ?

    The fuzzy % string is a paramter for sprintf which tells it what parameters to expect and where to place them.
    Basically, when programming you have integer/float numbers and you can calculate and do wonders with them - but you can't display them properly to the user. That's where sprintf function (and others) comes in.
    It formats other datatypes to you in a string.
    %c means character, %d means decimal (integer number).
    So where %c is, sprintf will place the character associated with 0x1b. Where %d are, sprintf will print the integer parameters.
    Note that the parameters after the format string must be in concurrence with the format string. If you tell it to print %c and then 2 %d's, then you need to supply these parameters in the correct datatype.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • cs05pp2cs05pp2 Member Posts: 3
    : Can someone explane to me what that means :
    : [code]: sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40) [/code]:
    :
    : I interesting most for :
    : "%c[%d;%d;%dm" ?
    : 0x1B ?

    I know all thinks you told me. I don't know what '[' and ';' and "%dm" means?
    Also "0x1B" what is mean?
  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    :I know all thinks you told me. I don't know what '[' and ';' and
    : "%dm" means?
    : Also "0x1B" what is mean?

    %dm = %d + m, eg sprintf(str, "%dm", 12) will give "12m"
    [ and ; are just characters.
    0x1B is a character. Something like ? I think.
    If you're curious what it all does, just create a simple test program and run it.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • ndixonndixon Member Posts: 5
    : :I know all thinks you told me. I don't know what '[' and ';' and
    : : "%dm" means?
    : : Also "0x1B" what is mean?
    :
    : %dm = %d + m, eg sprintf(str, "%dm", 12) will give "12m"
    : [ and ; are just characters.
    : 0x1B is a character. Something like ? I think.
    : If you're curious what it all does, just create a simple test
    : program and run it.
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry

    0x1B is the hex value of the ASCII ESC character - when you bang on the ESC key in a terminal window, that's the character that gets send to the host.

    In fact what this line of code is doing is to prepare an ANSI escape sequence which will set the foreground/background colors (and possibly bold/italic/underline attributes) of the the terminal.

    Most of the ANSI sequences begin with ESC then "[", followed by some parameter(s) and finally a character to indicate the command.

    The format string "%c[%d;%d;%dm" tells sprintf to interpret the following arguments as a character value and 3 integer values.

    The ";" delimits the parameters, which in this case are the attribute, foreground and background values.

    So if attr=1, fg=2 and bg=4, the resulting string is "^[[1;32;44m" (where ^[ represents the escape character), which when sent to an ANSI-compliant terminal would set up bold green text on a blue background.

    And to return to normal (white on black), you could send "^[[0;37;40m"


Sign In or Register to comment.