//[color=#FF0000][/color]一下方法可以解决你的问题
char* ApiReadFile(char *FileName,long PosMode,long ReadLen,long FlgGrp,long OffsetPos=0) //用API读文件
{
HANDLE handle;
DWORD Num;
char *ReadBuf=new char[ReadLen+1];
ReadBuf[ReadLen]='/0';
handle= ::CreateFile(FileName,GENERIC_READ,0,NULL,OPEN_ALWAYS, NULL,NULL);
if(INVALID_HANDLE_VALUE!= handle )
{
::SetFilePointer(handle,OffsetPos,NULL ,FILE_BEGIN);
::ReadFile(handle,ReadBuf,ReadLen,&Num,NULL);
::CloseHandle(handle);
}
else
;
return ReadBuf;
}
CString DecToHex2(long DecVal ) //二进制转十六进制
{
char buf[32];
ltoa(DecVal,buf,16); //
return buf;
}
void CBbbbDlg::OnButton1()
{
char * p;
CString aa;
//从文件开始读 6 个字节,当然从什么位置读,读多少个字节可以自定义
p=ApiReadFile("D://abc.txt",FILE_BEGIN,6,FILE_FLAG_DELETE_ON_CLOSE,0);
FILE *FileP;
FileP=fopen("D://dest.txt","w");
for(int i=0 ;i<6;i++)
{
aa=DecToHex2(p[i]);
fprintf(FileP,"%s/n",aa);
}
fclose(FileP);
delete p;
}
——————————————————
我省略了文件读写的部分,void Change(void *bin, int size, void *asc)只是一个转换函数,
bin 参数就是你用二进制读取了文件内容的缓冲区,size是这个缓冲区的长度,asc则是接受转换结果的缓冲区。
你可以这样
void main()
{
void Change(void *, size_t, void *);
FILE *src, *dst;
// 这里分别打开原始文件,以及目标文件,当然了,得用二进制,读写模式
char buffer[1024];
char *asc = buffer;
char *bin = buffer + 512;
while (!feof(src))
{
int num = fread(bin, 512, src);
Change(bin, num, asc);
fwrite(dst, num * 2, asc);
}
fclose(src);
fclose(dst);
}
void Change(void *bin, size_t size, void *asc)
{
static char tab[] = "0123456789ABCDEF";
unsigned char *src = (unsigned char *)bin;
unsigned char *dst = (unsigned char *)asc;
while (size-- > 0)
{
*dst++ = tab[*src >> 4];
*dst++ = tab[*src & 0xF];
++src;
}
}