Link to home
Start Free TrialLog in
Avatar of DanRaposo
DanRaposoFlag for United States of America

asked on

Very simple c++ question regarding cout

I have a very simple cout question for the group...

I have a c++ app that reads hex data off of a serial line...

If I do
printf("%x\n", readBuf[1]); I see exactly what I was expecting to see.

How do I do the same thing with c++.  I have tried
cout << hex << readBuf[1] << endl;

but that seems to just output ascii.

In this example, say this is what %x prints: 58, the cout line would print: W

Thanks for the quick help
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi DanRaposo,

You need to convert the ASCII to hex display.  fprintf(), sprintf(), Format(), or any of many available tools will do this for you.


Good Luck,
Kent
ASKER CERTIFIED SOLUTION
Avatar of Ichijo
Ichijo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is an exmple using streams.  - I compiled this using Visual Studio 2003


#include "stdafx.h"
#include <iostream>
#include <tchar.h>
#include <iostream>
#include <iomanip>

using namespace std;


//Showing the base of integer values
int _tmain(int argc, _TCHAR* argv[])
{
   int a=148;
   cout.setf(ios::showbase); // show the base of all integral output:
                             // leading 0x means hexadecimal,
                             // leading 01 to 07 means octal,
                             // leading 1 to 9 means decimal
   cout.setf(ios::oct,ios::basefield);
                             // change format state to octal
   cout << a << '\n';
   cout.setf(ios::dec,ios::basefield);
                             // change format state to decimal
   cout << a << '\n';
   cout.setf(ios::hex,ios::basefield);
                             // change format state to hexadecimal
   cout << a << '\n';
   cout << oct << a << '\n'; // Parameterized manipulators clear the
   cout << dec << a << '\n'; // basefield, then set the appropriate
   cout << hex << a << '\n'; // flag within basefield.

   return 0;
}
Avatar of DanRaposo

ASKER

wow... so pretty much just stick to the printf... it doesn't such a nice job compared to having to do anything in c++
>>>> C++ seems to get confused when you're outputting char data.
Don't think it is confusion.

There is an overloaded template function  operator<< (ostream& os, char c) which atually cannot print hex output. It simply ignores the 'hex' formatter stored in the istream. By casting it to a int, operator<< (ostream& os, int i) comes to play which does the required.

Note, printf has an advantage: when specifiying %x it expects an integer. Yu only pass a byte but that doesn't matter cause the arguments were aligned at a 32bit address padded with zeroes. So, the char passed with printf actually turns to a 32bit integer on the stack. Fortunately, cause otherwise the conversion could fail.

Regards, Alex
casting to int worked just fine

cout << hex << (int)readBuf[1] << endl;

Thank you
Hi Alex,

This is perhaps the ONLY sane argument for little-endian.  casting (int) to (char) is a NOOP.   :)


Kent
>>>> compared to having to do anything in c++
today I've seen 3 errors regarding printf and and scanf.

C++ streaming has the advantage that it is checked by the compiler while the printf and scanf are responsible for a big part of the security leaks we can encounter every enw day.
>>>> the ONLY sane argument for little-endian

Actually little-endian and big-endian do not differ regarding all kind of integers (char, short, int, long long). It doesn't matter whether you call the most 'left' or the most 'right' bit the 'low' bit. So, problems only arise if you need to convert the one to the other ...