Pages: [1]   Go Down
Author Topic: Convert int to char and append to buffer  (Read 992 times)
0 Members and 1 Guest are viewing this topic.
Norfolk UK
Offline Offline
Faraday Member
**
Karma: 71
Posts: 2611
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a char array that is partially filled with text and I want to convert an int to char and append to end of current text in the buffer. Is there a simple command to do this instead of converting int into another buffer and using strcat?

char buffer[100] = {"Something already there ="};

int number = 16;

//buffer=buffer+str(number)

// results in buffer containing "Something already there =16"
Logged


Germany
Offline Offline
Faraday Member
**
Karma: 59
Posts: 3083
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

itoa(number, buffer+strlen(buffer));
Logged

Norfolk UK
Offline Offline
Faraday Member
**
Karma: 71
Posts: 2611
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks michael_x,
I had almost got there with  itoa(x,chrBuffer[strlen(chrBuffer)],10); and wondering how to convert char to char* (I really don't get C programming with pointers).
Next question is how to append a space character?
Code:
   x=strlen(chrBuffer);
    chrBuffer[x] = " \0";
does not work.

« Last Edit: June 16, 2012, 09:11:56 am by Riva » Logged


UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    x=strlen(chrBuffer);
    chrBuffer[x] = ' ';
    chrBuffer[x+1] = '\0';
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Finland
Offline Offline
Sr. Member
****
Karma: 1
Posts: 270
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
strcpy(buffer+strlen(buffer), " ");

Edit: fixed param order
« Last Edit: June 16, 2012, 09:41:14 am by pekkaa » Logged

Finland
Offline Offline
Sr. Member
****
Karma: 1
Posts: 270
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I really don't get C programming with pointers.

I really think you should take the time and learn to understand the pointers. They are absolutely crucial in the C/C++ programming. And it's not that hard to learn really. 
Logged

Germany
Offline Offline
Faraday Member
**
Karma: 59
Posts: 3083
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

... the difference between char and char* is related to the difference between
'a' and "a" .
It's worth trying to understand that, too. 
Quote
And it's not that hard to learn really.
Yes smiley
Logged

Norfolk UK
Offline Offline
Faraday Member
**
Karma: 71
Posts: 2611
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
... the difference between char and char* is related to the difference between
'a' and "a" .
I understand the principal of pointers but it's the syntax that gets me.
I changed my code to use ' instead of " and it works but what is the special meaning of ' compared to " in C++
Also the arduino reference does not seem to mention little nuggets like itoa() and dtostrf(). Omissions like that steepens the learning curve.
« Last Edit: June 16, 2012, 11:05:22 am by Riva » Logged


UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

' denotes a character.  " denotes a pointer to an array of characters.

' is equivalent to a char, " is equivalent to a char* or a char[].
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Pages: [1]   Go Up
Jump to: