Search code examples
cconcatenationpicmicrochiphd44780

How to concatenate vars in c programmed pic?


I programming a 16f84a pic in hitech C to drive a hd44780 lcd. So far I've got the lcd initialized and can write individual characters and strings to the lcd. Now I need to do something like this:

var = 250;
lcd_write_string("MyVar has value: " + var);
so the lcd should show "MyVar has value: 250"

First of all how should I concatenate a var and a string? second, the variable var contains an 8 bit binary number (0-255 in decimal). If var = 23; the 8 bit number has to be split into 2 to represent the 2 and the 3 in ascii to then be shown by the lcd. how can I do this? It seems I have to do base 10 conversions or a if tree to split all 2 digit numbers to then be shown in the lcd. Is there an easier way around this?

thanks!


Solution

  • Why don't you just use printf("%d", var) ?

    var = 250;
    char *static_msg = "MyVar has value:";
    char msg[sizeof(static_msg) + 4];
    
    sprintf(msg, "%s %d", static_msg, var);
    lcd_write_string(msg);