Solved

can i do this without using an array

Posted on 2004-09-20
8
331 Views
Last Modified: 2010-04-01

#include <iostream>

using namespace std;

      int main() {
   
unsigned char character[4];
cout << "Please enter four ASCII characters: " ;
cin >> character;
   
int letter = 0 ;
letter = letter | ((int)character[0] << 24);
letter = letter | ((int)character[1] << 16);
letter = letter | ((int)character[2] << 8 ) ;
letter = letter | ((int)character[3]);

cout << " The value as Hex " << hex << letter;
 
cout << " The letters in reverse order:";

unsigned char numone, numtwo, numthree, numfour;
numone   = (unsigned char)(letter >> 24 );
numtwo   = (unsigned char)(letter >> 16 );
numthree = (unsigned char)(letter >> 8  );
numfour  = (unsigned char)(letter);
   
cout << numfour << "," << numthree << "," << numtwo << "," << numone ;
   
      return 0 ;
}
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
  • 2
  • +1
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12104583
Yes, you can:

#include <iostream>
#include <string>

using namespace std;

int main() {
   
string character;
cout << "Please enter four ASCII characters: " ;
getline(cin, character;);
   
char* pszCnvEnd = NULL;
int letter = 0 ;

letter = (int) strtol ( character.c_str(), &pcCnvEnd, 16);

//...
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12104802
> unsigned char character[4];
> cout << "Please enter four ASCII characters: " ;
> cin >> character;

This code won't work anyway because you need to reserve one extra character space for ending null-character. Must be:
unsigned char character[5];

jkr's code has a litle bug:
getline(cin, character;);
must be:
getline(cin, character);

There is not significative difference in using char[5] and string, last one is an array too (but encapsulated).
0
 
LVL 86

Expert Comment

by:jkr
ID: 12104873
>>jkr's code has a litle bug:
>>getline(cin, character;);
>>must be:
>>getline(cin, character);

Thanks for pointing that out :o)

I should also add that

#include <stdlib.h>

is necessary and the 'strtol()' line should actually read


letter = (int) strtol ( character.c_str(), &pszCnvEnd, 16);
0
Technology Partners: 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!

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12105165
Or that:

#include <iostream>

using namespace std;

int main()
{
    unsigned char characterF[4];
    unsigned char characterR[4];
    cout << "Please enter four ASCII characters: " << endl;

    for (int i = 0; i < 4; i++)
    {  
       cout << i << "-->";
       cin >> characterF[i];
       characterR[3-i] = characterF[i];
    }
    unsigned int letterR = *((unsigned int*)characterR);
   
    cout << " The value as Hex " << hex << letterR ;
    cout << " The letters in reverse order:";
    cout << characterF[0] << "," << characterF[1] << "," << characterF[2] << "," << characterF[3] ;
   
    return 0 ;
}

(tested)

Regards, Alex
0
 
LVL 3

Accepted Solution

by:
Kashra earned 500 total points
ID: 12106263
I think the following should work, without using the array, but I don't know why you wouldn't want to do so?

#include <iostream>

using namespace std;

int main() {

unsigned char character;
cout << "Please enter four ASCII characters: ";
character = cin.get();
   
int letter = 0 ;
letter = letter | ((int)character << 24);
character = cin.get();
letter = letter | ((int)character << 16);
character = cin.get();
letter = letter | ((int)character << 8 ) ;
character = cin.get();
letter = letter | ((int)character);

cout << " The value as Hex " << hex << letter;
 
cout << " The letters in reverse order:";

unsigned char numone, numtwo, numthree, numfour;
numone   = (unsigned char)(letter >> 24 );
numtwo   = (unsigned char)(letter >> 16 );
numthree = (unsigned char)(letter >> 8  );
numfour  = (unsigned char)(letter);
   
cout << numfour << "," << numthree << "," << numtwo << "," << numone ;
   
     return 0 ;
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 12112788
That cannot work:

unsigned char character;
cout << "Please enter four ASCII characters: ";
character = cin.get();

How would 4 characters fit in one unsigned char?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12113665
>>>> How would 4 characters fit in one unsigned char?

Simply by using the same character 4 times:

int letter = 0 ;
letter = letter | ((int)character << 24);
character = cin.get();
letter = letter | ((int)character << 16);
character = cin.get();
letter = letter | ((int)character << 8 ) ;
character = cin.get();
letter = letter | ((int)character);

Clever guy got all points for nothing.

Regards, Alex
0
 
LVL 3

Expert Comment

by:Kashra
ID: 12114372
Sorry guys, didn't mean to steal any points, just suggested a trivial answer. :)
0

Featured Post

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!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

734 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