一个面试的题目,比较专业,重谢!(200分)

  • 主题发起人 主题发起人 kingswang
  • 开始时间 开始时间
K

kingswang

Unregistered / Unconfirmed
GUEST, unregistred user!
这个问题对我来说太重要了,给点思路(最好有源码),请大侠帮忙,重谢!
Bin2TxtHex.exe
功能:二进制数据文件转换成文本16进制文件
命令格式:Bin2TxtHex.exe 二进制数据文件名 文本16进制文件名 文本中一行排列多少个数据
使用举例:
Bin2TxtHex.exe a.bin a.c 0x10
生成的数据格式
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
Bin2TxtHex.exe a.bin a.c 8
生成的数据格式
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
 
你太菜了!
void main(void)
{
FILE *fp;
int i,n;
char buffer[1024];
if ((fp=fopen(argv[2],"rb"))==NULL)
{
printf("input file not found/r/n");
exit(0);
}
i=atoi(arvg[3])
if (i==0)
{
printf("line size must greater 0/r/n");
fclose(fp);
exit(0);
}
while (fread(fp,buffer,i)==i)
{
for (n=0;
n<i;
n++)
{
printf("%.2x ",buffer[n]);
}
printf("/n");
}
//你去查一下fread的参数,如果文件长度不为i的整数倍,此处还需处理一下
fclose(fp);
}
 
谢谢TK128
刚转C++,什么都得从头来呀,太菜呀
 
VC++人气不旺呀,再没人知道吗?
 
我是这样做的,大家还有没有其它方法。
#include "stdafx.h"
#include <stdlib.h>
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <assert.h>
#include <stdio.h>

char Title[] = "File Tools - Bin2TxtHex";
//文件操作功能函数。
BOOL Size( char *pName, long* pSize );
BOOL Create( char *pName );
BOOL Read( char *pName, char *pBuffer,long offset, long size );
void tran(unsigned char ch, char str[4])
{
static char TABLE[17] = "0123456789ABCDEF";
str[0] = '0';
str[1] = 'X';
str[2] = TABLE[(ch &amp;
0xF0) >> 4];
str[3] = TABLE[(ch &amp;
0x0F)];
}
BOOL Bin2TxtHex( char*bFile, char*hFile, char*line )
{
char *pszBin = bFile; //二进制数据文件名
char *pszTxt = hFile; //十六制文本文件名
const int COLUMN_COUNT = ::atoi(line); //指定列数
FILE *hBin = ::fopen(pszBin, "r");
FILE *hTxt = ::fopen(pszTxt, "w");
assert(hBin != 0);
assert(hBin != 0);
char buf[1024];
int nColumn = 0;
while (!feof(hBin))
{
size_t count = ::fread(buf, sizeof(char), 1024, hBin);
char str[5];
for (char *pos = buf;
pos < buf + count;
pos++)
{
tran(*pos, str);
str[4] = ',';
::fwrite(str, sizeof(char), 5, hTxt);
if (COLUMN_COUNT == ++nColumn)
{
::fwrite("/n", sizeof(char), 1, hTxt);
nColumn = 0;
}
}
}
::fclose(hBin);
::fclose(hTxt);
return 0;
}
long atox( char*p )
{
long i = 0;
char c;
while(*p)
{
c = *p++;
if(c>='0'&amp;&amp;c<='9')c=c-'0';
else
if(c>='A'&amp;&amp;c<='F')c=c-('A'-10);
else
if(c>='a'&amp;&amp;c<='f')c=c-('a'-10);
else
return -1;
i = ((i<<4)|c);
}
return i;
}
long GetNumber( char*p )
{
if( p[0] =='0' &amp;&amp;
(p[1]=='x'||p[1]=='X') )
{
return atox( &amp;p[2] );
}
return (long)atoi( p );
}
//传入参数解析。
void ProcessCmd( char* pCmd )
{
char *p = pCmd;
char *maxp = p+strlen(p);
char *pStr[3];
long num;
for( int i=0;i<3;i++ )
{
while ( *p==' ' )p++;
if( !*p || p>=maxp )
{
::MessageBox(NULL,"命令参数不正确!操作终止!",Title,0);
return;
}
pStr = p;
while ( *p &amp;&amp;
*p!=' ' )p++;
*p++ = 0;
}
num = GetNumber( pStr[2] );
if( num<=0 )
{
::MessageBox(NULL,"命令参数不正确!操作终止!",Title,0);
return;
}
Bin2TxtHex( pStr[0], pStr[1], pStr[2] );
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
int num = strlen(lpCmdLine);
char *pCmdParm = new char[ num+1 ];
strcpy( pCmdParm, lpCmdLine );
ProcessCmd( pCmdParm );
delete []pCmdParm;
return 0;
}
 
再谢TK128
结贴
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
896
SUNSTONE的Delphi笔记
S
S
回复
0
查看
873
SUNSTONE的Delphi笔记
S
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
后退
顶部