[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Very simple c++ question regarding cout

Posted on 2007-10-11
9
Medium Priority
?
395 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:DanRaposo
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 20056911
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
0
 
LVL 4

Accepted Solution

by:
Ichijo earned 200 total points
ID: 20057001
You might try casting your readBuf[1] to int. C++ seems to get confused when you're outputting char data.

You may also find "<< setw(2) << setfill('0')" useful between the "hex" and the "readBuf[1]" so it always outputs 2 hex digits per char.
0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 20057018
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;
}
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!

 

Author Comment

by:DanRaposo
ID: 20057098
wow... so pretty much just stick to the printf... it doesn't such a nice job compared to having to do anything in c++
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20057138
>>>> 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
0
 

Author Comment

by:DanRaposo
ID: 20057166
casting to int worked just fine

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

Thank you
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 20057174
Hi Alex,

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


Kent
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20057212
>>>> 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.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20057277
>>>> 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 ...
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 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