Sunday 12 September 2010

Understand and reading binary files in C

I posted this tutorial quite a while back on dreamincode.net, here, so i won'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.

#include 
#include 

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 = "C:\\Users\\UrName\\Desktop\\testFile.bin"; 
 BYTE *fileBuf;   // Pointer to our buffered data
 FILE *file = NULL;  // File pointer

 // Open the file in binary mode using the "rb" format string
 // This also checks if the file exists and/or can be opened for reading correctly
 if ((file = fopen(filePath, "rb")) == NULL)
  cout << "Could not open specified file" << endl;
 else
  cout << "File opened successfully" << 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 < 100; i++)
  printf("%X ", fileBuf[i]);

 cin.get();
 delete[]fileBuf;
        fclose(file);   // Almost forgot this 
 return 0;
}

Thursday 9 September 2010

Nice and simple Binary to Decimal algorithm.

Little function for converting binary to decimal, as their seems to be a lack of these code snippets. here we go, enjoy.

#include 

/*  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 >= 0; i--)
 {
  dec += (binStr[i] == '1') ? pow : 0;
  pow *= 2;
 }
 return dec;
}

int main()
{
 std::cout << binToDec("1010");
 std::cin.get();
 return 0;
}

Tuesday 7 September 2010

Get CPU speed

This is some code that I "borrowed" from the PCSX2 source code and created a small program out of it, the PCSX2 source code is GNU so I don't think this is a problem. This code works nicely but will probably only compile in Visual C++, as the "__rdtsc()" function is microsoft specific.

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't be helped
#include 
#include 

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

__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) > 1) 
  {
   startTick = __rdtsc();
   break;
  }
 }

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

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

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

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

 getchar();
 return 0;
}

Welcome to my code snippets blog

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.

Syntax Highlighter test
while (!Dead())
{
 PostCodeSnippet();
 
 if (codeSnippetIsUseless)
  FeelBadMan();
 else
  FeelGoodMan();
}