Solved

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

Posted on 2004-09-19
6
215 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

747 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

13 Experts available now in Live!

Get 1:1 Help Now