Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using cout to print hex values; surely you can't be serious

Reply
Thread Tools

Using cout to print hex values; surely you can't be serious

 
 
gwowen
Guest
Posts: n/a
 
      02-09-2012
On Feb 9, 8:44*am, Ian Collins <(E-Mail Removed)> wrote:
> Don't forget the the format string was "%02X"..


Don't forget that width specifiers are a minumum. (Another talented,
concientious, experienced C-user, and *boom* another potential
sprintf() overflow bug).

sprintf() is horrid.
 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      02-12-2012
On Thu, 2012-02-09, Scott Lurndal wrote:
> gwowen <(E-Mail Removed)> writes:
>>On Feb 9, 8:44=A0am, Ian Collins <(E-Mail Removed)> wrote:
>>> Don't forget the the format string was "%02X"..

>>
>>Don't forget that width specifiers are a minumum. (Another talented,
>>concientious, experienced C-user, and *boom* another potential
>>sprintf() overflow bug).
>>
>>sprintf() is horrid.

>
> Which is why snprintf(3c) is preferred.


You still have to decide though what to do when you have truncation.
If you ignore the error and continue that might (in some cases) be
just as bad as the sprintf() buffer overflow.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
 
 
 
88888 Dihedral
Guest
Posts: n/a
 
      02-20-2012
在 2012年2月20日星期一UTC+8上午1时24分19秒 ,io_x写道:
> "gwowen" <(E-Mail Removed)> ha scritto nel messaggio
> news:(E-Mail Removed)...
> On Feb 8, 1:15 pm, Victor Bazarov <(E-Mail Removed)> wrote:
> > On 2/8/2012 12:16 AM, Chris Stankevitz wrote:
> >
> > Use 'printf', it seems to suit *this particular task* better.

>
> But sometimes you want to mix streaming objects with a custom
> operator<< with printing hex values of bytes. Are you suggesting
> interleaving


Well, after my carefully investigations of << and >>
, I bielive it is not so trivail in C++ to control.

For example, I have to set up bufers for a
data compressed encoder and decoder of various comprssion methods with options for fast speed RW operations or smaller sizes in the heap or the swap
files in the hd.

How do I pass the option?

Unless I design a control foarmt for the stream
or use some external methods to pass the options
to the stream operators, I can't think of better ways
in C++ stream operator reloading.

I am wondering is there other way for the >> and <<
operator reloadings.




>
> std::cout << complex_object;
> printf("%x",byte);
>
> and hope that buffering makes that OK?
>
> #io_x
> #yes if cout and printf use the same stream and the same stream buffer
>
> Alternatively, you may want to serialise some bytes to a
> std::stringstream or serialise some bytes to a stream provided by a
> library user as a std:stream& arguments, or to a file which has been
> opened as an std::fstream ... or ... or ...
>
> fprintf is fine, if you're writing to a C-style FILE*.
> It's useless for anything else, and there are *plenty* of anything-
> elses.
>
> The standard stream-formatting API is an bloody disaster area, so full
> of gotchas and weirdness as to be almost-unusable. Boost::Format is
> the solution.


 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      02-21-2012
88888 Dihedral於 2012年2月20日星期一UTC+8下午7時19分35秒 寫道:
> 在 2012年2月20日星期一UTC+8上午1时24分19秒 ,io_x写道:
> > "gwowen" <(E-Mail Removed)> ha scritto nel messaggio
> > news:(E-Mail Removed)....
> > On Feb 8, 1:15 pm, Victor Bazarov <(E-Mail Removed)> wrote:
> > > On 2/8/2012 12:16 AM, Chris Stankevitz wrote:
> > >
> > > Use 'printf', it seems to suit *this particular task* better.

> >
> > But sometimes you want to mix streaming objects with a custom
> > operator<< with printing hex values of bytes. Are you suggesting
> > interleaving

>
> Well, after my carefully investigations of << and >>
> , I bielive it is not so trivail in C++ to control.
>
> For example, I have to set up bufers for a
> data compressed encoder and decoder of various comprssion methods with options for fast speed RW operations or smaller sizes in the heap or the swap
> files in the hd.
>
> How do I pass the option?
>
> Unless I design a control foarmt for the stream
> or use some external methods to pass the options
> to the stream operators, I can't think of better ways
> in C++ stream operator reloading.
>
> I am wondering is there other way for the >> and <<
> operator reloadings.
>
>
>
>
> >
> > std::cout << complex_object;
> > printf("%x",byte);
> >
> > and hope that buffering makes that OK?
> >
> > #io_x
> > #yes if cout and printf use the same stream and the same stream buffer
> >
> > Alternatively, you may want to serialise some bytes to a
> > std::stringstream or serialise some bytes to a stream provided by a
> > library user as a std:stream& arguments, or to a file which has been
> > opened as an std::fstream ... or ... or ...
> >
> > fprintf is fine, if you're writing to a C-style FILE*.
> > It's useless for anything else, and there are *plenty* of anything-
> > elses.
> >
> > The standard stream-formatting API is an bloody disaster area, so full
> > of gotchas and weirdness as to be almost-unusable. Boost::Format is
> > the solution.


I studied the python bit-torrent long time ago. It is easy to set up sites
to join the torrents in Python. But how to do that in C++??
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
"<< hex" vs cout.setf(ios_base::hex) Generic Usenet Account C++ 2 07-12-2005 08:56 AM
std::cout vs cout Pmb C++ 2 06-02-2004 03:27 PM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM
man cout or info cout abi C++ 2 06-28-2003 06:42 PM



Advertisments