65,210
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include <stdlib.h>
#include <conio.h>
char *int_char(char *row,int r){/*将行数转换成字符串*/
int i=r,j=0;
i=i/10;
while(i!=0){
j++;
i=i/10;
}
for(i=j+1;i<8;i++)
*(row+i)=' ';/*保证行显示占8格*/
*(row+i)='\0';
for(i=j;i>=0;i--){
*(row+i)=r%10+48;
r/=10;
}
return row;
}
int main(){
FILE *fin,*fout;
char filename[50],ch1,ch2,row[10];
int i=0,r=1;/*r代表行数*/
printf("请输入\(路径\)文件名:");
scanf("%s",filename);
if((fin=fopen(filename,"rb"))==0){
printf("文件打开错误!可能文件不存在!\n");
getch();
exit(0);
}
fout=fopen("查看文件的16进制代码.txt","w");
ch1=fgetc(fin);/*取得一个字符,即8bit*/
while(!feof(fin))
{
if(i==0){
fputs(int_char(row,r),fout);
r++;
}
/////////////////////////////////////////////////////////////////////////////////////////
ch2=15;
ch2&=ch1>>4;/*取得ch1的后前位*/
if(ch2>=0 && ch2<=9)
fputc(ch2+48,fout);/*输出*/
else
fputc(ch2+55,fout);
/////核心代码
ch2=ch1 & 15;/*取得ch1的后4位*/
if(ch2>=0 && ch2<=9)
fputc(ch2+'0',fout);/*输出*/
else
fputc(ch2+55,fout);
///////////////////////////////////////////////////////////////////////////////////////////
i++;
if(i==8){
fputc(' ',fout);
fputc(' ',fout);
}
else if(i==16){
fputc('\n',fout);
i=0;
}///////////////////////////////////////把每一个字符的16进制码分开
else fputc(' ',fout);
ch1=fgetc(fin);
}
fclose(fin);
fclose(fout);
puts("生成文件成功!!!");
} string context;
ifstream in("C:\\test.txt",ios::in);
while(in >> context)
{
for(int i = 0; i < context.size(); i++)
{
int data = context[i];
cout << hex << data;
}
}
fscanf(fp,"%x",s);