Pages: [1]   Go Down
Author Topic: int to char question  (Read 682 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 155
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would like to display some information on NKC display

this code does what you would think:

Code:
myGLCD.print("blue:", LEFT, 70,255,255,255);

I've googled to try and figure this question out but have not been successful.

I have several int variables that I need to display.  This just displays garbage:
Code:
int chnlD = 1;
 char *c;
 c=(char *)chnlD;

Any suggestions would be great!
Thanks for checking in,
Loren
Logged

USA
Offline Offline
Full Member
***
Karma: 0
Posts: 238
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use sprintf or itoa.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 612
Posts: 49216
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
char *c;
 c=(char *)chnlD;
c is a pointer. It doesn't point to anything. You can not put a value in the space it (doesn't) point to that way. You should not point to one type (int) with a pointer of another type (char), unless you really know what you are doing.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 155
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guys,

Here is what I've tried without any luck:

Code:
int red = 255;
sprintf(c, "%d", red);
Do I understand sprintf correctly?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 612
Posts: 49216
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Do I understand sprintf correctly?
Hard to say without seeing how you defined c.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 155
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry here it is:

Code:
int red = 255;
char *c;
sprintf(c, "%d", red);
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 612
Posts: 49216
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Then, no, you are not using sprintf() correctly.

The way you have c defined, it is a pointer. What does it point to? Nothing. And, yet, you as sprintf() to write to the memory pointed to by c.
Code:
char c[4];
sprintf(c, "%d", red);
would work.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 155
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 
Then, no, you are not using sprintf() correctly.

The way you have c defined, it is a pointer. What does it point to? Nothing. And, yet, you as sprintf() to write to the memory pointed to by c.
Code:
char c[4];
sprintf(c, "%d", red);
would work.

It does work...but only partially
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26273
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It does work...but only partially
That's...partially helpful
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 155
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry I was in the middle of four things....  for a moment it was only displaying a zero. 

I had some other code botched.  Now it is working as it should.  Thanks for all of the help.
Logged

Pages: [1]   Go Up
Jump to: