Solved

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

Posted on 2004-09-19
6
216 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:tedschnieders
  • 3
  • 2
6 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 12097601
You can store them in a standard buffer and convert to hex strings later. Something like this:

char buffer[500];  <--- You must dimension proper size
gets(buffer);

int len = strlen(buffer);
buffer[len+1] = buffer[len+2] = buffer[len+3] = 0;   // ensure 4-byte alignment

unsigned int pack;
for (int i=0; i<len; i+=4) {
    pack = *((int *)(buffer+i));
    printf ("%08X ", pack);
}
0
 

Author Comment

by:tedschnieders
ID: 12097718
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.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12097772
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:tedschnieders
ID: 12097852
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++
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12098075
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.
0
 
LVL 1

Expert Comment

by:zekard
ID: 12098859
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;

}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now