Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,926 questions

51,859 answers

573 users

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 265 views
1 answer 153 views
2 answers 215 views
2 answers 368 views
368 views asked Dec 29, 2021 by avibootz
...