From: ryot AT bigfoot DOT com (George Ryot) Newsgroups: comp.os.msdos.djgpp Subject: Re: Convert int to string?? Message-ID: <37a0b9ee.2311940@news.clara.net> References: <199907281451 DOT KAA28958 AT delorie DOT com> <99072810315800 DOT 00592 AT dome DOT calderathin DOT com> X-Newsreader: Forte Agent 1.5/32.452 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Thu, 29 Jul 1999 21:07:24 GMT NNTP-Posting-Host: 195.8.91.111 X-Complaints-To: abuse AT clara DOT net X-Trace: nnrp3.clara.net 933282444 195.8.91.111 (Thu, 29 Jul 1999 22:07:24 BST) NNTP-Posting-Date: Thu, 29 Jul 1999 22:07:24 BST Lines: 31 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Darren Noble wrote: > ther is a funtion itoa. you sould use it like this: > > itoa(palette_color_r, text, 10); // 10 being the base you want the intiger to > // be int the string > On Wed, 27 Jan 1999, you wrote: ^^^^^ is this correct? Deja does not have this post. > > I want to convert an integer to > > a string. I can convert a string to an integer using atoi but there seems > > to be no c library function to do the opposite. So it's either so simple > > char text[3]; > > int palette_color_r; > > text[3] = palette_color_r; // I can't cast so what? No you can't just cast it, and in any case text[3] refers to an element outside the bounds of that array. Use atoi() as above or do: sprintf(text, "%d", palette_color_r); sprintf() has the advantage of being an ANSI/POSIX function, you can also format the number if required. char text[3] does not allocate enough storage to cater for all possible values of the int, even if the value is not expected to be more than 255 this situation should probably be allowed for. -- george