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
Solved

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

Posted on 2004-09-19
6
218 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 Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Getting IP address 8 89
How can i compile this github project?? 2 90
How to measure current at integrated circuit (IC) power pin using Current Probe ? 6 32
Outlook 13 41
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

860 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