How to display (output) text file in hex using C++

1 Answer

0 votes
#include <iostream>
#include <fstream>
#include <iomanip>

using std::cout;
using std::endl;

#define SIZE 64

int main()
{
	std::ifstream ifs("d:\\data.bin", std::ios::in | std::ios::binary);

	if (!ifs) {
		cout << "Error open file" << endl;
		return 1;
	}

	char arr[SIZE];
	int i;

	cout.setf(std::ios::uppercase);
	while (!ifs.eof()) {
		for (i = 0; i < SIZE && !ifs.eof(); i++)
			ifs.get(arr[i]);
		for (int j = 0; j < i - 1; j++)
			cout << std::setw(3) << std::hex << (int)arr[j];

		cout << endl;
	}

	ifs.close();

	return 0;
}

/*
run:

2D  0  0  0  8  0  0  0  C  0  0  0 62  0  0  0 6B  3  0  0

*/

 



answered Jun 9, 2018 by avibootz

Related questions

1 answer 301 views
1 answer 167 views
2 answers 245 views
2 answers 400 views
400 views asked Dec 29, 2021 by avibootz
...