C++显示文件十六进制

  • Post author:
  • Post category:其他


有时候总会想看文件的十六进制,但打开其他的软件我觉得非常的消耗内存,所以用C++实现了个

#include<iostream>
#pragma warning(disable:4996)
using namespace std;
char fl[] = "+-/*\\()&^%$#@!~`?<>,.|{}_=\"\':;";
bool is(char t)
{
	for (int i = 0; i < 31; i++)
	{
		if (t == fl[i])
			return true;
	}
	return false;
}
void main(int argc,char **argv)
{
	if (argc == 1)
	{
		cout << "没有输入文件"<<endl;
		return;
	}
	FILE *fp = fopen(argv[1], "rb");
	if (fp == NULL)
	{
		cout << "文件打开失败!" << endl;
		return;
	}
	char t[16];
	int len = 0;
	int k = 0;
	int h = 0;
	while (len = fread(t, 1, 16, fp))
	{
		printf("%06X  ", k++);
		for (int i = 0; i < len; i++)
		{
			printf("%02X ", (int)(unsigned char)t[i]);
		}
		printf("   ");
		for (int i = 0; i < len; i++)
		{
			if ((t[i] >= '0'&&t[i] <= '9') || (t[i] >= 'A'&&t[i] <= 'Z') || (t[i] >= 'a'&&t[i] <= 'z'))
				printf("%c", t[i]);
			else if (is(t[i]))
				printf("%c", t[i]);
			else
				printf(".");
		}
		if (h++ == 28)
		{
			h = 0;
			int c = getchar();
			if (c == 'C"' || c == 'c')
				return;
		}
		printf("\n");
	}
	fclose(fp);
}



版权声明:本文为qq_42877866原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。