Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #13863

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: Using cout to print hex values; surely you can't be serious
Date Thu, 09 Feb 2012 20:22:00 +1300
Lines 29
Message-ID <9phagoFom1U7@mid.individual.net> (permalink)
References <08081227-b09b-454f-b23d-85ebb98207c0@n12g2000yqb.googlegroups.com> <Xns9FF44420CAC16myfirstnameosapriee@216.196.109.131>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net WGW175qc+Ptk4LI7u+TSVQ38p+yGU2xlVeWSBZu+LMjz3/PhjX
Cancel-Lock sha1:Xk+vNBqEIsdw0p+UAHDuPpDrTSA=
User-Agent Mozilla/5.0 (X11; SunOS i86pc; rv:6.0.2) Gecko/20111003 Thunderbird/6.0.2
In-Reply-To <Xns9FF44420CAC16myfirstnameosapriee@216.196.109.131>
Xref x330-a1.tempe.blueboxinc.net comp.lang.c++:13863

Show key headers only | View raw


On 02/ 9/12 07:41 PM, Paavo Helde wrote:
> Chris Stankevitz<chrisstankevitz@gmail.com>  wrote in news:08081227-b09b-
> 454f-b23d-85ebb98207c0@n12g2000yqb.googlegroups.com:
>
>> char c = 0xFF;
>>
>> Q1: How do I use cout to print the variable c so that it appears at
>> stdout as "FF"?
>
> char buff[3];
> sprintf(buff, "%02X", c);
> std::cout<<  buff;
>
> If you are afraid that char is 32 or 64 bits on some platform and might not
> fit into the buffer, you can make the buffer e.g. 17 bytes long as a
> precaution.

The buffer is an array of char, so it will always be big enough no 
matter how big a char is (in c++ terms it is always 1).

> The point is that the dreaded buffer overflows associated with
> sprintf() can be avoided here easily as the output size is constrained, so
> there is no reason why sprintf() could not be used if it suits the task
> better.

Completely agree.

-- 
Ian Collins

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Using cout to print hex values; surely you can't be serious Chris Stankevitz <chrisstankevitz@gmail.com> - 2012-02-07 21:16 -0800
  Re: Using cout to print hex values; surely you can't be serious gwowen <gwowen@gmail.com> - 2012-02-08 03:54 -0800
  Re: Using cout to print hex values; surely you can't be serious Victor Bazarov <v.bazarov@comcast.invalid> - 2012-02-08 08:15 -0500
    Re: Using cout to print hex values; surely you can't be serious gwowen <gwowen@gmail.com> - 2012-02-08 06:49 -0800
      Re: Using cout to print hex values; surely you can't be serious "io_x" <a@b.c.invalid> - 2012-02-19 18:24 +0100
        Re: Using cout to print hex values; surely you can't be serious 88888 Dihedral <dihedral88888@googlemail.com> - 2012-02-20 03:19 -0800
          Re: Using cout to print hex values; surely you can't be serious 88888 Dihedral <dihedral88888@googlemail.com> - 2012-02-21 03:08 -0800
    Re: Using cout to print hex values; surely you can't be serious Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-02-08 15:20 +0000
  Re: Using cout to print hex values; surely you can't be serious Leigh Johnston <leigh@i42.co.uk> - 2012-02-08 14:20 +0000
  Re: Using cout to print hex values; surely you can't be serious Paavo Helde <myfirstname@osa.pri.ee> - 2012-02-09 00:41 -0600
    Re: Using cout to print hex values; surely you can't be serious Ian Collins <ian-news@hotmail.com> - 2012-02-09 20:22 +1300
      Re: Using cout to print hex values; surely you can't be serious gwowen <gwowen@gmail.com> - 2012-02-09 00:16 -0800
        Re: Using cout to print hex values; surely you can't be serious Ian Collins <ian-news@hotmail.com> - 2012-02-09 21:44 +1300
          Re: Using cout to print hex values; surely you can't be serious Juha Nieminen <nospam@thanks.invalid> - 2012-02-09 10:36 +0000
          Re: Using cout to print hex values; surely you can't be serious gwowen <gwowen@gmail.com> - 2012-02-09 03:56 -0800
            Re: Using cout to print hex values; surely you can't be serious scott@slp53.sl.home (Scott Lurndal) - 2012-02-09 17:06 +0000
              Re: Using cout to print hex values; surely you can't be serious Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-02-12 15:59 +0000
    Re: Using cout to print hex values; surely you can't be serious scott@slp53.sl.home (Scott Lurndal) - 2012-02-09 17:04 +0000

csiph-web