Link to home
Start Free TrialLog in
Avatar of tedschnieders
tedschnieders

asked on

i need to know how to write a simple program that reads ascII characters in c++

i need to know how to write a simple program that reads ascII characters from the keyboard packs them into a simple four byte integer and then display the value as hex

if someone can please help me
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tedschnieders
tedschnieders

ASKER

I have this assignment and we are dealing with bitwise operators

The assignment that i cant figgure out is i have to create a program that reads four ascII characters from the keyboard and packs them into a single four byte integer. then display the value as hex and output them in reverse order with low byte first.
a simple "brute force" starting point:

char c1,c2,c3,c4;

c1 = getch();
c2 = getch();
c3 = getch();
c4 = getch();

unsigned int pack;

pack = c1 | (c2<<8) | .....   // etcetera
can you just show me how you can use cout<< and cin>> to get the information and ouput it in hex

sorry i am a beginner and not to familiar with C++
to display an hex number:

int yournumber;
// some processing here
cout <<hex << yournumber;

To trap keys use getch() as shown, or use:

string buffer;
cin.getline(buffer);

To read an entire buffer and process later.
tedschnieders,

two ways of doing this easy:

#include <iostream>

using namespace std;

int main()
{

     cout << "Please enter four ASCII characters: ";
     char Num[500];  //<--- You must dimension proper size
     gets(Num);

     // do something ...


     // integer array
     int iNum[500];
     for ( int i = 0; i < strlen ( Num ); i++ )
     {
          iNum[i] = Num[i];
     }

     for ( i = 0; i < strlen ( Num ); i++ )
     {
          cout << "0x" << hex << iNum[i] << endl;
     }

     return 0;

}

OR

#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{

     cout << "Please enter four ASCII characters: ";
     char Num[500];  //<--- You must dimension proper size
     gets(Num);

     // do something ...

     for ( int i = 0; i < strlen ( Num ); i++ )
     {
          printf ( "0x%x\n", Num[i] );
     }
     return 0;

}