<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>int to string (char) conversion in C</title>
		<link>http://www.allegro.cc/forums/view/588784</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Wed, 29 Nov 2006 20:14:09 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hello all. I am new to C programming and was wondering if it is possible to convert an integer value to char or string. I checked a book, but only found documentation to convert from string to integer. Thanks in advance.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bob Keane)</author>
		<pubDate>Tue, 28 Nov 2006 22:58:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>A char is an integer type already.<br /><span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_737.html" target="_blank">sprintf</a></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Tue, 28 Nov 2006 23:01:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>int to char is done by simply casting:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">int</span> i <span class="k3">=</span> <span class="n">10</span><span class="k2">;</span>
<span class="k1">char</span> c<span class="k2">;</span>
c <span class="k3">=</span> i<span class="k2">;</span>
</pre></div></div><p>

Remember that chars can hold 256 numbers only. As for string, just use sprintf, like <i>sprintf(buffer, &quot;%d&quot;, i);</i>, where buffer is a char array and i an integer. Remember to make the buffer as large as necessary.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ReyBrujo)</author>
		<pubDate>Tue, 28 Nov 2006 23:06:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If you want to convert from an integer to a string with a specified radix, use <tt>itoa()</tt>.</p><p><span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_517.html" target="_blank">itoa</a><span class="k2">(</span><span class="k2">)</span></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Tue, 28 Nov 2006 23:30:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for the help. If I were adding the numeric value to the end of an existing string, would the format be:</p><p>char buffer[16} = &quot;&lt;some characters&gt;&quot;;<br />int place;<br />char c;<br />c = sprintf(buffer, &quot;%d&quot;, place);?</p><p>[/edit] I tried itoa, got an undefined reference to itoa.<br />[edit/]
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bob Keane)</author>
		<pubDate>Tue, 28 Nov 2006 23:47:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Nah, just:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">char</span> buffer<span class="k2">[</span><span class="n">16</span><span class="k2">}</span> <span class="k3">=</span> <span class="s">"&lt;some characters&gt;"</span><span class="k2">;</span>
<span class="k1">int</span> place <span class="k3">=</span> <span class="n">10</span><span class="k2">;</span>

<a href="http://www.delorie.com/djgpp/doc/libc/libc_737.html" target="_blank">sprintf</a><span class="k2">(</span>buffer, <span class="s">"%d"</span>, place<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Tue, 28 Nov 2006 23:51:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That worked. Thanks LennyLen and X-G.</p><p>[\edit] Oops, closed the thread too soon. It appears to be overwriting what is in buffer, as opposed to adding place to the end. Any ideas?<br />[\edit]
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bob Keane)</author>
		<pubDate>Tue, 28 Nov 2006 23:59:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I tried itoa, got an undefined reference to itoa
</p></div></div><p>

You need to add the following to the top of your code:</p><p><span class="source-code"><span class="p">#include &lt;stdlib.h&gt;</span></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Wed, 29 Nov 2006 00:13:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>&lt;stdlib.h&gt; was included. Is it recent? Maybe my compiler does not support it yet.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bob Keane)</author>
		<pubDate>Wed, 29 Nov 2006 00:17:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It&#39;s not new, but it&#39;s not an ANSI C funtion either, though most compilers support it. Which are you using?</p><p>The following compiled fine for me using GCC.</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="p">#include &lt;stdio.h&gt;</span></td></tr><tr><td class="number">2</td><td><span class="p">#include &lt;stdlib.h&gt;</span></td></tr><tr><td class="number">3</td><td>&#160;</td></tr><tr><td class="number">4</td><td><span class="k1">char</span> buffer<span class="k2">[</span><span class="n">20</span><span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">5</td><td><span class="k1">int</span> n<span class="k2">;</span></td></tr><tr><td class="number">6</td><td>&#160;</td></tr><tr><td class="number">7</td><td><span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">8</td><td>  </td></tr><tr><td class="number">9</td><td>  n <span class="k3">=</span> <span class="n">25</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td>  </td></tr><tr><td class="number">11</td><td>  <a href="http://www.delorie.com/djgpp/doc/libc/libc_517.html" target="_blank">itoa</a><span class="k2">(</span>n, buffer, <span class="n">2</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">12</td><td>  <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"%d in binary is %s\n"</span>, n, buffer<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>  </td></tr><tr><td class="number">14</td><td>  <a href="http://www.delorie.com/djgpp/doc/libc/libc_517.html" target="_blank">itoa</a><span class="k2">(</span>n, buffer, <span class="n">16</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">15</td><td>  <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"%d in hexadecimal is %s\n"</span>, n, buffer<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>  </td></tr><tr><td class="number">17</td><td>  <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">18</td><td>  </td></tr><tr><td class="number">19</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Wed, 29 Nov 2006 00:40:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I am using gcc 4.0.2. I copied the test program you submitted and compiled, still having problems with the itoa function. Are you coding in C or C++? I coded in C.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bob Keane)</author>
		<pubDate>Wed, 29 Nov 2006 01:35:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I compiled as C. I&#39;m currently using GCC 3.4.2, so perhaps it&#39;s been removed in later versions.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Wed, 29 Nov 2006 01:41:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>New rule: no creating new programming forum threads until you read the thread list!</p><p>Is this week the printf assignment at the college that you three attend?</p><p><a href="http://www.allegro.cc/forums/thread/588773">http://www.allegro.cc/forums/thread/588773</a><br /><a href="http://www.allegro.cc/forums/thread/588701">http://www.allegro.cc/forums/thread/588701</a></p><p>Not that I&#39;m being hostile or anything.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Wed, 29 Nov 2006 01:43:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>CGamesPlay: My origional question was how to convert an int to char or string. To get back on topic, I tried sprintf, the new characters overwrote what was in the target string. Is there any way to add the characters to the end of the string, or did I do something wrong?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bob Keane)</author>
		<pubDate>Wed, 29 Nov 2006 02:34:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre><span class="p">#include &lt;stdio.h&gt;</span>
<span class="p">#include &lt;string.h&gt;</span>

<span class="k1">char</span> buffer<span class="k2">[</span><span class="n">20</span><span class="k2">]</span>, buffer2<span class="k2">[</span><span class="n">20</span><span class="k2">]</span><span class="k2">;</span>

<span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span>
  
     n <span class="k3">=</span> <span class="n">25</span><span class="k2">;</span>
     <a href="http://www.delorie.com/djgpp/doc/libc/libc_756.html" target="_blank">strcpy</a><span class="k2">(</span>buffer, <span class="s">"stuff"</span><span class="k2">)</span><span class="k2">;</span>
     <a href="http://www.delorie.com/djgpp/doc/libc/libc_737.html" target="_blank">sprintf</a><span class="k2">(</span>buffer2, <span class="s">"%d"</span>, n<span class="k2">)</span><span class="k2">;</span>
     <a href="http://www.delorie.com/djgpp/doc/libc/libc_752.html" target="_blank">strcat</a><span class="k2">(</span>buffer, buffer2<span class="k2">)</span><span class="k2">;</span>
  
     <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>

<span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Wed, 29 Nov 2006 02:40:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That was it. Thanks.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bob Keane)</author>
		<pubDate>Wed, 29 Nov 2006 20:14:09 +0000</pubDate>
	</item>
</rss>
