Products > Programming

actually using itoa() with Peter Fleury's LCD library

(1/3) > >>

Hopefully this topic will get some decent replies and anyone else like me will find it in the future and find it helpful  :popcorn:

I need to print a variables value on an LCD. Peters library can only print characters and strings presumably relying on itoa() rather than rewrite what already exists:

lcd_puts (const char *s)

char* itoa   (   int    val,
char *    s,
int    radix

So what do I do with this? I have an 8 bit value (0-100) that I need to put on the screen. Do I do something like:

--- Code: ---
lcd_puts (itoa(my_value, out_put, 4);

--- End code ---

I think I have seen the output (char * s) setup as an array on other sites trying to explain it.

I don't think you want radix 4 - is that supposed to be the string length?

If so, try something like this:

--- Code: ---char buf[5];

sprintf(buf, "%4d", my_value);
--- End code ---

sorry radix should have been 10. I need a buffer of 4, 3 characters plus the string termination

so is "s" supposed to be the buffer length? and the function returns the start address of the string?

The third parameter in itoa() is the base, so you would probably want to have 10 there instead of 4.

You need to have empty array for the resulting string, and give pointer to that as parameter to the itoa.

--- Code: ---int my_value = 3;
char display_message[5] = {""};

itoa(my_value, display_message, 10);

--- End code ---

You could also use sprintf, if you want to have leading zero's or other formatting to the number string.

--- Code: ---int my_value = 3;

char display_message[5] = {""};
sprintf(display_message, "%d", my_value);


--- End code ---

If you want to have it with leading zeros and 4 digit precision, you can format the sprintf like this:

--- Code: ---sprintf(display_message, "%04d", my_value);
--- End code ---


[0] Message Index

[#] Next page

There was an error while thanking
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod