text file ASCII converting

Dec 12, 2013 at 12:31am
hello all,
i am new and in need of assistance. i am trying to convert the contents of a .txt to ASCII values and save it as a separate .txt file. im not sure how to do this. can anyone help me?
Dec 12, 2013 at 1:15am
So are you saying that the encoding of your input is not ASCII, or that you want to convert each character to a number and save it?
Dec 12, 2013 at 1:35am
yes, i want to convert each character.
Dec 12, 2013 at 1:41am
You're misunderstanding several concepts here. Forget about ASCII completely, just erase it from your mind.

Files are made up of bytes. You can interpret a file however you want - be it as text, or as a bunch of numbers, but the content is the same either way.
Dec 12, 2013 at 1:44am
ok so then i want to change the bytes?
Dec 12, 2013 at 1:45am
No, you want to interpret the bytes as numbers, and convert those numbers to strings with spaces between each one. You won't be 'changing' anything.
Last edited on Dec 12, 2013 at 1:45am
Dec 12, 2013 at 1:48am
ok, i think i understand that now. how would i tell the program to read it differently?
Last edited on Dec 12, 2013 at 1:51am
Dec 12, 2013 at 2:12am
All you have to do is tell your program to write it differently. Read your file into a buffer, then extract from the buffer one character at a time.

http://www.parashift.com/c++-faq/print-char-or-ptr-as-number.html
Dec 12, 2013 at 2:37am
Thats helpful, thanks. Now how do i get it to read from an external file?
Dec 12, 2013 at 6:04pm
the fstream class. http://www.cplusplus.com/reference/fstream/fstream/ There are also a pile of examples you can look up easily enough.
Dec 12, 2013 at 6:29pm
Also there is a tutorial on file input and output:
http://www.cplusplus.com/doc/tutorial/files/

I'd suggest you may find the get() function useful for reading one character at a time from the input file. Then you simply need to output that char as an int, and repeat.
http://www.cplusplus.com/reference/istream/istream/get/
Topic archived. No new replies allowed.