Converting 32 bit intiger to printable 8 bit Character

Aug 24, 2012 at 6:54am
I want to convert a series of 32-bit integer values into a sequence of printable 8-bit character values. Mapping the 32-bit integers to printable 8-bit character values should result in a clear ASCII art image.

I can convert Integer to ASCII:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main(){
	char ascii;
	int numeric;
	
	cout << "Enter Number ";
	cin >> numeric;
	cout << "The ascii value of " << numeric << " is  " << (char) numeric<<"\n\n"<<endl;
	return 0;
}



Also I need to open the text file that my numbers are saved into:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("1.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}


but my problem is , I can not open this " Text " file and print the ASCII on the screen and also print a copy of that in a " Output.txt "

Inside of my Text file is just :

757935403 544999979 175906848 538976380
757795452 170601773 170601727

That after converting to ASCII needs to look like this :

represents the ASCII art picture

+---+
|   |
|   |
+---+

and have this also in my output.txt.

Please advise if you know how can I write this program.

Thanks
Last edited on Aug 24, 2012 at 8:08am
Aug 24, 2012 at 11:56pm
Thanks
Aug 25, 2012 at 2:24pm
The problem is in you reading the file.

You simply cout the entire line, instead, you should iterate over the file by the ::get() member function. You push this back into a string. Once you encounter a non-number character (In your case, a space), you need to int unit[x] = std::atoi(std::string). This converts the string to a number (integer). Once this is done, you can cast that integer to a char (You'll lose the last 24 bits tho, so having bits in any of the last 24 is useless, your range should be 0-255). Then you can std::cout that character.
Topic archived. No new replies allowed.