Solved

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

Posted on 2004-09-19
6
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

695 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