<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Compiled Thoughts by John Ciacia &#187; C</title>
	<atom:link href="http://www.johnciacia.com/category/snippets/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.johnciacia.com</link>
	<description>Science, Technology, and Beyond</description>
	<lastBuildDate>Fri, 06 Jan 2012 15:46:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Convert text to an image</title>
		<link>http://www.johnciacia.com/2011/07/02/convert-text-to-an-image/</link>
		<comments>http://www.johnciacia.com/2011/07/02/convert-text-to-an-image/#comments</comments>
		<pubDate>Sat, 02 Jul 2011 06:17:56 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">http://www.johnciacia.com/?p=1243</guid>
		<description><![CDATA[Inspired by this post, I decided to write a file to bmp converter (because I have nothing better to do with my life). [code lang="c"]//because I can... #include &#60;stdio.h&#62; #include &#60;stdlib.h&#62; #include &#60;stdint.h&#62; #include &#60;string.h&#62; int main(int argc, char **argv) { if(argc != 3) printf(&#34;%s &#60;input-file&#62; &#60;output-file&#62;\n&#34;, argv[0]); FILE *input_file, *output_file; long size; char *buffer; [...]]]></description>
			<content:encoded><![CDATA[<p>Inspired by <a href="http://stackoverflow.com/questions/5508110/why-is-this-program-erroneously-rejected-by-three-c-compilers/5509538#5509538">this post</a>, I decided to write a file to bmp converter (because I have nothing better to do with my life).</p>
<p>[code lang="c"]//because I can...<br />
#include &lt;stdio.h&gt;<br />
#include &lt;stdlib.h&gt;<br />
#include &lt;stdint.h&gt;<br />
#include &lt;string.h&gt;</p>
<p>int main(int argc, char **argv) {</p>
<p>	if(argc != 3)<br />
		printf(&quot;%s &lt;input-file&gt; &lt;output-file&gt;\n&quot;, argv[0]);</p>
<p>	FILE *input_file, *output_file;<br />
	long size;<br />
	char *buffer;<br />
	size_t result;</p>
<p>	input_file = fopen(argv[1], &quot;rb&quot;);<br />
	if(input_file == NULL) { fputs (&quot;File error&quot;, stderr); exit(1); }</p>
<p>	fseek(input_file, 0, SEEK_END);<br />
	size = ftell(input_file);<br />
	rewind(input_file);</p>
<p>	buffer = (char*)malloc(sizeof(char)*size);<br />
	if (buffer == NULL) { fputs(&quot;Memory error&quot;, stderr); exit (2); }</p>
<p>	result = fread(buffer, 1, size, input_file);<br />
	if (result != size) { fputs(&quot;Reading error&quot;, stderr); exit(3); }</p>
<p>	uint8_t header[54] =<br />
		{<br />
			'B', 'M', 	//2		signature<br />
			0,0,0,0,	//6		size of BMP file in bytes<br />
			0,0,		//8  	reserved must be zero<br />
			0,0,		//10	reserved must be zero<br />
			54,0,0,0,	//14	offset to start of image data<br />
			40,0,0,0,	//18	size of BITMAPINFOHEADER structure, must be 40<br />
			0,0,0,0,	//22	image width in pixels<br />
			0,0,0,0,	//26	image height in pixels<br />
			1,0,		//28	number of planes in the image, must be 1<br />
			24,0,		//30	number of pits per pixel (1, 4, 8, or 24)<br />
			0,0,0,0		//34	compression type<br />
						//54	useless stuff<br />
		};</p>
<p>	//Set the image size<br />
	header[2] = (uint8_t)(size + sizeof header);<br />
	header[3] = (uint8_t)((size + sizeof header) &gt;&gt; 8);<br />
	header[4] = (uint8_t)((size + sizeof header) &gt;&gt; 16);<br />
	header[5] = (uint8_t)((size + sizeof header) &gt;&gt; 24);</p>
<p>	//Do something fancy calculation for with the width/height<br />
	header[18] = (uint8_t)(size/3);	//image width<br />
	header[22] = (uint8_t)(1);		//image height</p>
<p>	output_file = fopen(argv[2], &quot;wb&quot;);<br />
	fwrite(header, sizeof header, 1, output_file);<br />
	fwrite(buffer, size + sizeof header, 1, output_file);</p>
<p>	fclose(input_file);<br />
	fclose(output_file);<br />
	free (buffer);<br />
	return 0;<br />
}[/code]</p>
<p>[code]dhcp197-066:~ johnciacia$ cat hello.c<br />
#include &lt;stdio.h&gt;</p>
<p>int main(void) {<br />
	printf(&quot;Hello, World!\n&quot;);<br />
	return 0;<br />
}</p>
<p>dhcp197-066:~ johnciacia$ cat hello.bmp<br />
BM?6(#include &lt;stdio.h&gt;</p>
<p>int main(void) {<br />
	printf(&quot;Hello, World!\n&quot;);<br />
	return 0;<br />
}</p>
<p>dhcp197-066:~ johnciacia$ hexdump hello.bmp<br />
0000000 42 4d 84 00 00 00 00 00 00 00 36 00 00 00 28 00<br />
0000010 00 00 1a 00 00 00 01 00 00 00 01 00 18 00 00 00<br />
0000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00<br />
0000030 00 00 00 00 00 00 23 69 6e 63 6c 75 64 65 20 3c<br />
0000040 73 74 64 69 6f 2e 68 3e 0a 0a 69 6e 74 20 6d 61<br />
0000050 69 6e 28 76 6f 69 64 29 20 7b 0a 09 70 72 69 6e<br />
0000060 74 66 28 22 48 65 6c 6c 6f 2c 20 57 6f 72 6c 64<br />
0000070 21 5c 6e 22 29 3b 0a 09 72 65 74 75 72 6e 20 30<br />
0000080 3b 0a 7d 0a 00 00 00 00 00 00 00 00 00 00 00 00[/code]<br />
(there is a small problem somewhere that leads to extra padding at the end, but oh well, they are null bytes)</p>
<p><a href="http://www.johnciacia.com/wp-content/uploads/2011/07/Screen-shot-2011-07-02-at-2.13.26-AM.png"><img src="http://www.johnciacia.com/wp-content/uploads/2011/07/Screen-shot-2011-07-02-at-2.13.26-AM.png" alt="" title="Screen shot 2011-07-02 at 2.13.26 AM" width="488" height="374" class="aligncenter size-full wp-image-1244" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnciacia.com/2011/07/02/convert-text-to-an-image/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>itoa</title>
		<link>http://www.johnciacia.com/2010/04/09/itoa/</link>
		<comments>http://www.johnciacia.com/2010/04/09/itoa/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 20:02:20 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://www.johnciacia.com/?p=507</guid>
		<description><![CDATA[Convert an integer to an ASCII string [code lang="cpp"]/* K&#038;R2 60-63 */ char *itoa(int n, char s[]) { int c, i, j, sign; if((sign = n) < 0) n = -n; do { s[i++] = n % 10 + '0'; } while((n /= 10) > 0); if(sign < 0) s[i++] = '-'; s[i] = '\0'; [...]]]></description>
			<content:encoded><![CDATA[<p>Convert an integer to an ASCII string</p>
<p>[code lang="cpp"]/* K&#038;R2 60-63 */<br />
char *itoa(int n, char s[])<br />
{<br />
    int c, i, j, sign;</p>
<p>    if((sign = n) < 0)<br />
        n = -n;</p>
<p>    do {<br />
        s[i++] = n % 10 + '0';<br />
    } while((n /= 10) > 0);</p>
<p>    if(sign < 0)<br />
        s[i++] = '-';</p>
<p>    s[i] = '\0';</p>
<p>    for (i = 0, j = strlen(s)-1; i < j; i++, j--) {<br />
        c = s[i];<br />
        s[i] = s[j];<br />
        s[j] = c;<br />
    }</p>
<p>    return s;<br />
}<br />
[/code]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnciacia.com/2010/04/09/itoa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: www.johnciacia.com @ 2012-02-05 11:16:38 -->
