Hi,
I'm trying to convert an int to a char array then append another char on the end. This is to be outputted to an LCD. I keep getting stuck when trying to follow examples on the web.
long number = x; //value from user input 0 to 2million
char buf[12]; // Output buffer
char suffix[2]; // suffix for appending to number (k or M)
int thousands = (number/1000);
suffix[0] = 'k';
itoa(reducedNo, buf, 10); // convert number to text
buf+=suffix; //invalid operands to binary operator+
My brain has melted now. Does anyone have a good way to do this without using String?
buf+=suffix; //invalid operands to binary operator+
char arrays can't be concatenated with the + operator. You can use strcat(), although you will need to ensure suffix is a valid c-style string (Needs to be null-terminated, you don't explicitly make it). You can also use sprintf like UKHeliBob suggested, or you can simply do this:
Get the length of the string:
int len = strlen(buf);
"Append" 'k' to the string by replacing the null with your character
Thanks. Sorry I missed something quite important!
There should be a line..
float reducedNo = number/1000
And i should have been asking how to convert a float! [facepalm]
So this means sprintf wont work as it does not like floats on the arduino.
itoa also wants an int
Basically what I am trying to do is display a frequency on an LCD that can be anything from 0 to 2 million. I'm hoping to make the larger numbers easier to read by making them look like 10k instead of 10000.
RichMo:
Basically what I am trying to do is display a frequency on an LCD that can be anything from 0 to 2 million. I'm hoping to make the larger numbers easier to read by making them look like 10k instead of 10000.