阅读背景:

每日一算法:二进制文件的处理

来源:互联网 
// BinToHex.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "BinToHex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	CString FileNameSrc = argv[1];
	CString FileNameTag = FileNameSrc + ".h";
	FILE *fp1;
	FILE *fp2;
	int ch;
	int count = 0;
	
	fp1 = fopen(FileNameSrc,"rb");
	if (!fp1)
	{
		return 0;
	}
	fp2 = fopen(FileNameTag,"wb+");
	fputs("const unsigned char BintoHex[]={ \r\n\r\n",fp2);
	while (((ch = fgetc(fp1))) != EOF)
	{//每读取一个字符,在它前面添加0x,后面添加 , 号,
		fputs("0x",fp2);
		if (ch <= 0xf)
		{
				fputc('0',fp2);
		}
		fprintf(fp2,"%X",ch);
		fputc(',',fp2);
		count++;
		if (count % 16 == 0)
		{
			fputs("\r\n",fp2);
		}

	}
	fputs("\r\n}",fp2);
	fclose(fp1);
	fclose(fp2);
	return 0;
}// BinToHex.cpp : Defines the entry point for t



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: