Sorry for the confusing title, as I am new to C++

Basically, what I am trying to do is, to read a simple test.txt file's content, which are just two words actually: "Hello World" and desplay thier hexadecimal content in a shell, CMD or command line.(Whatever it is called), if the text thing is confusing you here, just think of it, as if I am trying to open a simple .wav file and read it's data and display it on CMD, like you would using

#include <iostream>
#include <fstream>

using namespace std;

int main(){

  fstream myFile;

  myFile.open("test.txt");

  //reading goes here, the problem is displaying the hex data on the editor (CMD    )

  return 0;
}

I am new to this, and downloaded codeblocks, so I can only work with the CMD as of now. I can open files with the fstream class, and write in it, but sadly I can not read it's hexa content.

Is there anyway to do this by simple means in C++?

You can read the file either the entire line at one time then step through the string and convert each character to hex, or you can read the file one character at a time in a loop, convert it to hex then write it back to the console screen. For now it might be easier if you read the file one characger at a time.

char c;

while( myfile >> c )
{
   // print the character in hex format.  You need not do any reformatting yourself, let cout do it for you

   std::cout << std::hex << c << " ";
}

Edited 3 Years Ago by Ancient Dragon

This article has been dead for over six months. Start a new discussion instead.