File data to ASCII guidance.

I'm a c++ beginner.
I have a classwork which I have tried to complete for a few days now and I'm still confused.

I have a file called Data.txt .
It contains
757935403 544999979 175906848 538976380
757795452 170601773

What I have to do is take those 32 bit integer values and covert them into a 4 byte char. Which in the console will display an ASCII art image.
They are asking me to also learn how to split an 32 bit integer into 4 ASCII char.

This url will show the art image properly, Its out of place in the thread
http://prntscr.com/aotizp

This is the art image:
+---+
| |
| |
+---+
So I tried converting it to Hexadecimal first then to convert them into ASCII.
I will try show you how I began my code but change it if you want to, because im just 4 weeks into learning c++.

Please help thank you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
unsigned a;
ifstream file("E:\\Data.txt", std::ios_base::in);
while (file >> a)
{
cout << setw(8) << setfill('0') << hex << a;
return 0;
}
Last edited on
**Update**
I tried some ways and I got my code like this . But the output doesn't seem to position my ASCII characters in the proper place.

I will link the way it shows in my console incase its my compiler going nuts and I don't realise or something.
http://prntscr.com/aotont

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cctype>
using namespace std;

int main (int argc, char* argv[])
{
	typedef char byte;
	ifstream file("E:\\SampleData.txt", std::ios_base::in);
	unsigned a;
	
	while (file >> a)
	{
	 	char signs[4];
	 	int i;
	 	for (i = 0; i <= 4; i++)
	 	{		
	 		signs[i] = (a>>16)&0xff;
	 		cout << signs[i] ;
	 	}
	 }
		return 0;	
}

	
One way to do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <fstream>
#include <windows.h>

using namespace std;

void display (unsigned input)
{
  WORD highWord = HIWORD (input);
  WORD lowWord = LOWORD (input);

  BYTE Byte1 = HIBYTE (highWord);
  BYTE Byte2 = LOBYTE (highWord);
  BYTE Byte3 = HIBYTE (lowWord);
  BYTE Byte4 = LOBYTE (lowWord);

  cout << Byte1 << Byte2 << Byte3 << Byte4 << '\n';
}
int main ()
{
  unsigned input;
  ifstream file ("E:\\Data.txt", std::ios_base::in);
  if (!file)
  {
    cerr << "\a\n*** File error: ***\n";
    exit (EXIT_FAILURE);
  }
  while (file >> input)
  {
    display (input);
  }
  system ("pause");
  return EXIT_SUCCESS;
}

Output

---+
|
+

|
|
-+
|

+--


Probably there is an easier way to do it, but my lunch break is coming to an end.
Awesome thanks for refining the code. However the Output is not synchronized according required one, I will try something from there and update it. Also the
1
2
3
4
5
6
7
WORD highWord = HIWORD (input);
  WORD lowWord = LOWORD (input);

  BYTE Byte1 = HIBYTE (highWord);
  BYTE Byte2 = LOBYTE (highWord);
  BYTE Byte3 = HIBYTE (lowWord);
  BYTE Byte4 = LOBYTE (lowWord);


is there a place where I can read up on how the lines actually function?
Thank You.
The first value in the file was 757935403 which in hex is 2D2D2D2B.

HIWORD gets 2 first (high) two bytes which are 2D2D.

HIBYTE get the first (high) byte which is 2D.

http://forums.codeguru.com/showthread.php?302806-what-is-HIBYTE-LOBYTE-HIWORD-LOWORD
Can be pretty confusing if you don't know about bit operations.
One way to get a byte from an int is to use bitwise operations, a combination of shifting and masking will do the job. Rather than write the whole program, here are some ideas/example to start off with. I've not given a lot of explanation, but the first part is an example, then the second part shows one of the samples from the original question.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

using namespace std;

int main() 
{

    unsigned example = 0x12345678; // hexadecimal is easier to read/understand here
    unsigned mask = 0xFF;          // in binary this is 11111111, all bits are one.
    
    cout << hex;
    
    cout << "example = " << example << '\n';   // entire original value
    
    cout << (example & mask) << '\n';          // low-order byte, use bitwise AND operator &
    cout << ((example >> 8) & mask) << '\n';   // >> is right shift operator
    cout << ((example >> 16) & mask) << '\n';  
    cout << ((example >> 24) & mask) << '\n';  // high-order byte
    
    
    
    cout << "\n\n Output as a character\n";
    unsigned num = 757935403;
    
    cout << char(num & mask) << '\n';          // low-order character    
    cout << char((num >> 8) & mask) << '\n';   
    cout << char((num >> 16) & mask) << '\n';  
    cout << char((num >> 24) & mask) << '\n';  // high-order character
   
}

Output:
example = 12345678
78
56
34
12


 Output as a character
+
-
-
-


For more detail on bitwise operations, see http://www.cprogramming.com/tutorial/bitwise_operators.html

Last edited on
Hey guys I have completed the code that required to display the integers from the file into the console window as ASCII characters.

At the moment I am still currently in the works of modifying the code to put in other lines to replace the code in void display() because my syllabus never covered it. Well its part of my brain forcing me to try complete it in my own way. But I learned a few methods just from this on how to display this output. Thanks again guys.
Specifically thanks [Thomas1965 and Chervil].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <fstream>
#include <windows.h>

using namespace std;

void display (unsigned input)
{
  WORD highWord = HIWORD (input);
  WORD lowWord = LOWORD (input);

  BYTE Byte1 = HIBYTE (highWord);
  BYTE Byte2 = LOBYTE (highWord);
  BYTE Byte3 = HIBYTE (lowWord);
  BYTE Byte4 = LOBYTE (lowWord);

  cout << Byte4 << Byte3 << Byte2 << Byte1;
}
int main ()
{
  unsigned input;
  ifstream file ("E:\\Data.txt", std::ios_base::in);
  if (!file)
  {
    cerr << "Unable to open file. ";
    return 1;
  }
  while (file >> input)
  {
    display (input);
  }
  return 0;
}



My output is in a printscreen link, not sure how to display that here.
*This output is used to display another input file which contains shit tons of integers*
****FINAL UPDATE****
This is the redone code which I found would match my syllabus also.
Thank you cplusplus community you guys are awesome.






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>

using namespace std;

void display (unsigned input)
{
unsigned mask = 0xFF; 

   cout << char(input & mask) ;              
   cout << char((input >> 8) & mask) ;   
   cout << char((input >> 16) & mask) ;
   cout << char((input >> 24) & mask) ;

  
}
int main ()
{
  unsigned input;
  ifstream file ("E:\\Data.txt", std::ios_base::in);
  if (!file)
  {
    cerr << "Unable to open file. ";
    return 1;
  }
  while (file >> input)
  {
    display (input);
  }
  return 0;
}
Topic archived. No new replies allowed.