简体   繁体   中英

Importing from .txt and converting to hexadecimal then exporting and vice versa C++

This is my first project, I'm trying to teach myself so please bear with me and don't assume anything about my knowledge of programming.

What I want to do is create a console program that will take file, Text.txt, in this instance, read it, turn the ASCII it into hexadecimal and then overwrite the Text.txt file with the hex code, and if run again, will reverse the process.

Is there a way to do this on a command basis so that I have to type something, lets say "Start" into the console before it will actually initialize this process? And on top of that, keep the console open so I could type "Start" (or another word, if this is impossible) and have it undo the process?

I currently have this tacked onto the end to keep the program open, but how would I use it when doing what I wish to do as mentioned above? Or is there a better method? [code] std::string in;

while ((std::cin >> in) && (in != "exit"))
{
}
return 0;
}

I know I need to use fstream in order to import and export but I'm not sure how to do it without closing everything immediately after it has finished or how to tie it in to make it convert the hexadecimal/ASCII first.

Thanks for any help or tips you can give.

Edit: Sorry, my question is:

What is the basic process for turning the ASCII in the Text.txt file into hex and then exporting it? I have a basic understanding of the fstream concept on its own but I'm not really sure how to implement it along with the conversion of the text, and I don't really know where to start on exactly how to convert the text into hex.

You've got several questions bundled here, and I think you might just be overwhelmed.

  1. Is there a way to do this on a command basis so that I have to type something
  2. How would I use it when doing what I wish to do as mentioned above?
  3. but I'm not sure how to do it without closing everything immediately after it has finished
  4. how to tie it in to make it convert the hexadecimal/ASCII first.

Start by decomposing the problem into parts, and implement each, one at a time. First: file I/O -- create a function (or better still: a class) which reads from a file.

Next: add a function or method that convert values from ASCII to hexadecimal.

Next: add a class or function which implements the terminal control features/curses/etc.

Separate these and they'll be easier to design, easier to test, and easier to get help from SO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM