<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8039981275495897841</id><updated>2024-09-04T04:26:08.288-07:00</updated><title type='text'>Code Snippets</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://cppcodesnippets.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8039981275495897841/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://cppcodesnippets.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Ryan</name><uri>http://www.blogger.com/profile/13161303486072318544</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='33' height='26' src='http://4.bp.blogspot.com/_aYI8XKQ7NZc/THxLCpZjWBI/AAAAAAAAABM/mHltonO0zSM/S220/center+swirl+blue+1600x1280.png'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8039981275495897841.post-1190136377468135244</id><published>2010-09-12T15:46:00.000-07:00</published><updated>2010-09-12T15:46:02.278-07:00</updated><title type='text'>Understand and reading binary files in C</title><content type='html'>I posted this tutorial quite a while back on dreamincode.net, &lt;a href=&quot;http://www.dreamincode.net/forums/topic/170054-understanding-and-reading-binary-files-in-c/&quot;&gt;here&lt;/a&gt;, so i won&#39;t repost it, no point, but i will post the code snippet that i provided with the tutorial. It is mainly targetted and those who wish to read in the binary information contained within a file, for use in emulation for example, and to teach the difference between text files and binary files.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;#include &lt;stdio.h&gt;
#include &lt;iostream&gt;

using namespace std;

// An unsigned char can store 1 Bytes (8bits) of data (0-255)
typedef unsigned char BYTE;

// Get the size of a file
long getFileSize(FILE *file)
{
 long lCurPos, lEndPos;
 lCurPos = ftell(file);
 fseek(file, 0, 2);
 lEndPos = ftell(file);
 fseek(file, lCurPos, 0);
 return lEndPos;
}

int main()
{
 const char *filePath = &quot;C:\\Users\\UrName\\Desktop\\testFile.bin&quot;; 
 BYTE *fileBuf;   // Pointer to our buffered data
 FILE *file = NULL;  // File pointer

 // Open the file in binary mode using the &quot;rb&quot; format string
 // This also checks if the file exists and/or can be opened for reading correctly
 if ((file = fopen(filePath, &quot;rb&quot;)) == NULL)
  cout &lt;&lt; &quot;Could not open specified file&quot; &lt;&lt; endl;
 else
  cout &lt;&lt; &quot;File opened successfully&quot; &lt;&lt; endl;

 // Get the size of the file in bytes
 long fileSize = getFileSize(file);

 // Allocate space in the buffer for the whole file
 fileBuf = new BYTE[fileSize];

 // Read the file in to the buffer
 fread(fileBuf, fileSize, 1, file);

 // Now that we have the entire file buffered, we can take a look at some binary infomation
 // Lets take a look in hexadecimal
 for (int i = 0; i &lt; 100; i++)
  printf(&quot;%X &quot;, fileBuf[i]);

 cin.get();
 delete[]fileBuf;
        fclose(file);   // Almost forgot this 
 return 0;
}
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://cppcodesnippets.blogspot.com/feeds/1190136377468135244/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cppcodesnippets.blogspot.com/2010/09/understand-and-reading-binary-files-in.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8039981275495897841/posts/default/1190136377468135244'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8039981275495897841/posts/default/1190136377468135244'/><link rel='alternate' type='text/html' href='http://cppcodesnippets.blogspot.com/2010/09/understand-and-reading-binary-files-in.html' title='Understand and reading binary files in C'/><author><name>Ryan</name><uri>http://www.blogger.com/profile/13161303486072318544</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='33' height='26' src='http://4.bp.blogspot.com/_aYI8XKQ7NZc/THxLCpZjWBI/AAAAAAAAABM/mHltonO0zSM/S220/center+swirl+blue+1600x1280.png'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8039981275495897841.post-6735605260971298554</id><published>2010-09-09T15:00:00.000-07:00</published><updated>2010-09-09T15:00:56.863-07:00</updated><title type='text'>Nice and simple Binary to Decimal algorithm.</title><content type='html'>Little function for converting binary to decimal, as their seems to be a lack of these code snippets. here we go, enjoy.&lt;br /&gt;
&lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;cpp&quot;&gt;#include &lt;iostream&gt;

/*  This function takes a binary string and returns
    the decimal equivalent using the Positional notation  
    method. */
__int64 binToDec(char *binStr)
{
 __int64 pow = 1, dec = 0;
 for (int i = strlen(binStr)-1; i &gt;= 0; i--)
 {
  dec += (binStr[i] == &#39;1&#39;) ? pow : 0;
  pow *= 2;
 }
 return dec;
}

int main()
{
 std::cout &lt;&lt; binToDec(&quot;1010&quot;);
 std::cin.get();
 return 0;
}
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://cppcodesnippets.blogspot.com/feeds/6735605260971298554/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cppcodesnippets.blogspot.com/2010/09/nice-and-simple-binary-to-decimal.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8039981275495897841/posts/default/6735605260971298554'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8039981275495897841/posts/default/6735605260971298554'/><link rel='alternate' type='text/html' href='http://cppcodesnippets.blogspot.com/2010/09/nice-and-simple-binary-to-decimal.html' title='Nice and simple Binary to Decimal algorithm.'/><author><name>Ryan</name><uri>http://www.blogger.com/profile/13161303486072318544</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='33' height='26' src='http://4.bp.blogspot.com/_aYI8XKQ7NZc/THxLCpZjWBI/AAAAAAAAABM/mHltonO0zSM/S220/center+swirl+blue+1600x1280.png'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8039981275495897841.post-2193742163771105980</id><published>2010-09-07T20:53:00.000-07:00</published><updated>2010-09-07T20:53:36.253-07:00</updated><title type='text'>Get CPU speed</title><content type='html'>This is some code that I &quot;borrowed&quot; from the PCSX2 source code and created a small program out of it, the PCSX2 source code is GNU so I don&#39;t think this is a problem. This code works nicely but will probably only compile in Visual C++, as the &quot;__rdtsc()&quot; function is microsoft specific. &lt;br /&gt;
&lt;br /&gt;
Sorry about the way the HTML interprets the include directives as code tags and terminates them at the end of the code by the way... can&#39;t be helped&lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;cpp&quot;&gt;#include &lt;stdio.h&gt;
#include &lt;windows.h&gt;

// Link the library we need for the timeGetTime() function
#pragma comment (lib, &quot;WINMM.LIB&quot;)

__int64 getCPUSpeed(unsigned int time)
{
    __int64 timeStart, timeStop;
    __int64 startTick, endTick;
    __int64 overhead;

 overhead = __rdtsc() - __rdtsc();
 
 timeStart = timeGetTime();
 while(timeGetTime() == timeStart) 
    {
  timeStart = timeGetTime();
    }
 for(;;)
 {
  timeStop = timeGetTime();
  if ((timeStop - timeStart) &gt; 1) 
  {
   startTick = __rdtsc();
   break;
  }
 }

 timeStart = timeStop;
 for(;;)
 {
  timeStop = timeGetTime();
  if ((timeStop - timeStart) &gt; time) 
  {
   endTick = __rdtsc();
   break;
  }
 }

 return (__int64)((endTick - startTick) + (overhead));
}

int main()
{
 char *cpuSpeed = (char*)malloc(24);
 sprintf(cpuSpeed, &quot;%d&quot;, getCPUSpeed(1000) / 1000000);  // Get CPU speed

 printf(&quot;CPU Speed: %c.%c%c%cGhz&quot;, cpuSpeed[0], cpuSpeed[1], cpuSpeed[2], cpuSpeed[3]);

 getchar();
 return 0;
}
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://cppcodesnippets.blogspot.com/feeds/2193742163771105980/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cppcodesnippets.blogspot.com/2010/09/get-cpu-speed.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8039981275495897841/posts/default/2193742163771105980'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8039981275495897841/posts/default/2193742163771105980'/><link rel='alternate' type='text/html' href='http://cppcodesnippets.blogspot.com/2010/09/get-cpu-speed.html' title='Get CPU speed'/><author><name>Ryan</name><uri>http://www.blogger.com/profile/13161303486072318544</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='33' height='26' src='http://4.bp.blogspot.com/_aYI8XKQ7NZc/THxLCpZjWBI/AAAAAAAAABM/mHltonO0zSM/S220/center+swirl+blue+1600x1280.png'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8039981275495897841.post-213961915627933480</id><published>2010-09-07T20:42:00.000-07:00</published><updated>2010-09-07T20:44:27.691-07:00</updated><title type='text'>Welcome to my code snippets blog</title><content type='html'>This will be a blog for posting random code snippets and maybe some tutorials too. I just got the syntax highlighter working on Blogger, but sorry about the code formatting, it only seems to indent the code by 1 space, which is kinda annoying, but oh well.&lt;br /&gt;
&lt;br /&gt;
Syntax Highlighter test&lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;cpp&quot;&gt;while (!Dead())
{
 PostCodeSnippet();
 
 if (codeSnippetIsUseless)
  FeelBadMan();
 else
  FeelGoodMan();
}
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://cppcodesnippets.blogspot.com/feeds/213961915627933480/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cppcodesnippets.blogspot.com/2010/09/welcome-to-my-code-snippets-blog.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8039981275495897841/posts/default/213961915627933480'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8039981275495897841/posts/default/213961915627933480'/><link rel='alternate' type='text/html' href='http://cppcodesnippets.blogspot.com/2010/09/welcome-to-my-code-snippets-blog.html' title='Welcome to my code snippets blog'/><author><name>Ryan</name><uri>http://www.blogger.com/profile/13161303486072318544</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='33' height='26' src='http://4.bp.blogspot.com/_aYI8XKQ7NZc/THxLCpZjWBI/AAAAAAAAABM/mHltonO0zSM/S220/center+swirl+blue+1600x1280.png'/></author><thr:total>0</thr:total></entry></feed>