Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: How to use std::cout to output a char as a number?

Reply
Thread Tools

Re: How to use std::cout to output a char as a number?

 
 
dan.furlani@earthling.net
Guest
Posts: n/a
 
      10-01-2007
On Apr 30, 7:52 am, Dancefire <(E-Mail Removed)> wrote:
> Hi, everyone
>
> It might be a simple question, but I really don't know the answer.
>
> charc = '1';cout<< c;
>
> The above code will only output a '1' rather than 0x31;


A quick and dirty solution:

class MyOs {
public:
MyOs(ostream &os) : os_(os) { }

template <typename T>
ostream & operator<<(T thing) { os_ << thing; return os_; }

ostream & operator<<(char ch) // treat as unsigned.
{ os_ << static_cast<unsigned short>(static_cast<unsigned
char>(ch)); return os_; }
ostream & operator<<(signed char ch)
{ os_ << static_cast<signed short>(ch); return os_; }
ostream & operator<<(unsigned char ch)
{ os_ << static_cast<unsigned short>(ch); return os_; }

private:
ostream &os_;
};

enum Fix_Char { fix_char };
MyOs operator<<(ostream &os, Fix_Char f) { return MyOs(os); }


Now you can do this:

template <typename T>
void foo(T arg) {
cout << "test " << fix_char << arg << endl;
}

I think that does what Dancefire wants but it sure is ugly. Can
someone please suggest the correct way to implement this?

-Dan

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-01-2007
* http://www.velocityreviews.com/forums/(E-Mail Removed):
> On Apr 30, 7:52 am, Dancefire <(E-Mail Removed)> wrote:
>> Hi, everyone
>>
>> It might be a simple question, but I really don't know the answer.
>>
>> charc = '1';cout<< c;
>>
>> The above code will only output a '1' rather than 0x31;

>
> A quick and dirty solution:
>
> class MyOs {
> public:
> MyOs(ostream &os) : os_(os) { }
>
> template <typename T>
> ostream & operator<<(T thing) { os_ << thing; return os_; }
>
> ostream & operator<<(char ch) // treat as unsigned.
> { os_ << static_cast<unsigned short>(static_cast<unsigned
> char>(ch)); return os_; }
> ostream & operator<<(signed char ch)
> { os_ << static_cast<signed short>(ch); return os_; }
> ostream & operator<<(unsigned char ch)
> { os_ << static_cast<unsigned short>(ch); return os_; }
>
> private:
> ostream &os_;
> };
>
> enum Fix_Char { fix_char };
> MyOs operator<<(ostream &os, Fix_Char f) { return MyOs(os); }
>
>
> Now you can do this:
>
> template <typename T>
> void foo(T arg) {
> cout << "test " << fix_char << arg << endl;
> }
>
> I think that does what Dancefire wants but it sure is ugly. Can
> someone please suggest the correct way to implement this?


Uh,

char c = '1';
cout << '0x' << hex << c+0;

should do the trick.

"+0" causes a conversion to int.


Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      10-02-2007
On Oct 2, 1:47 am, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * (E-Mail Removed):


> > On Apr 30, 7:52 am, Dancefire <(E-Mail Removed)> wrote:
> >> It might be a simple question, but I really don't know the answer.


> >> charc = '1';cout<< c;


> >> The above code will only output a '1' rather than 0x31;


[...]
> Uh,


> char c = '1';
> cout << '0x' << hex << c+0;


> should do the trick.


> "+0" causes a conversion to int.


Shades of Fortran. If you want a conversion to int, why don't
you say so:

std::cout << hex << (int)c ;

(For those not familiar with Fortran, adding 0 to an argument
was a traditional way of simulating pass by value, which Fortran
didn't have otherwise.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-02-2007
* James Kanze:
> On Oct 2, 1:47 am, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
>> * (E-Mail Removed):

>
>>> On Apr 30, 7:52 am, Dancefire <(E-Mail Removed)> wrote:
>>>> It might be a simple question, but I really don't know the answer.

>
>>>> charc = '1';cout<< c;

>
>>>> The above code will only output a '1' rather than 0x31;

>
> [...]
>> Uh,

>
>> char c = '1';
>> cout << '0x' << hex << c+0;

>
>> should do the trick.

>
>> "+0" causes a conversion to int.

>
> Shades of Fortran. If you want a conversion to int, why don't
> you say so:


I did.


> std::cout << hex << (int)c ;


Not a good habit.


> (For those not familiar with Fortran, adding 0 to an argument
> was a traditional way of simulating pass by value, which Fortran
> didn't have otherwise.)


I don't understand how anybody can be unfamiliar with Fortran. Common
blocks, RATFOR... Ah.

Cheers,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-02-2007
Alan Woodland wrote:
> James Kanze wrote:
>>> char c = '1';
>>> cout << '0x' << hex << c+0;

>>
>>> should do the trick.

>>
>>> "+0" causes a conversion to int.

>>
>> Shades of Fortran. If you want a conversion to int, why don't
>> you say so:
>>
>> std::cout << hex << (int)c ;

> C-Style casts?
>
> surely
>
> static_cast<int>(c);
>
> or
>
> int(c);
>
> would better still?


I wonder why *in this particular case* it would be better. Care
to elaborate? And let's stick to technical C++ issues instead of
"perpetuating good habits" kind of argument. Thanks!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
anon
Guest
Posts: n/a
 
      10-02-2007
Victor Bazarov wrote:
> Alan Woodland wrote:
>> James Kanze wrote:
>>>> char c = '1';
>>>> cout << '0x' << hex << c+0;
>>>> should do the trick.
>>>> "+0" causes a conversion to int.
>>> Shades of Fortran. If you want a conversion to int, why don't
>>> you say so:
>>>
>>> std::cout << hex << (int)c ;

>> C-Style casts?
>>
>> surely
>>
>> static_cast<int>(c);
>>
>> or
>>
>> int(c);
>>
>> would better still?

>
> I wonder why *in this particular case* it would be better. Care


You made me wonder, so I have to ask:
Why it wouldn't?

> to elaborate? And let's stick to technical C++ issues instead of
> "perpetuating good habits" kind of argument. Thanks!

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-02-2007
anon wrote:
> Victor Bazarov wrote:
>> Alan Woodland wrote:
>>> James Kanze wrote:
>>>>> char c = '1';
>>>>> cout << '0x' << hex << c+0;
>>>>> should do the trick.
>>>>> "+0" causes a conversion to int.
>>>> Shades of Fortran. If you want a conversion to int, why don't
>>>> you say so:
>>>>
>>>> std::cout << hex << (int)c ;
>>> C-Style casts?
>>>
>>> surely
>>>
>>> static_cast<int>(c);
>>>
>>> or
>>>
>>> int(c);
>>>
>>> would better still?

>>
>> I wonder why *in this particular case* it would be better. Care

>
> You made me wonder, so I have to ask:
> Why it wouldn't?


There is no difference, AFAICT. When there is no difference, there
can be no "better" or "worse". Of cousre, I may not know all the
details, so I am asking why it would, to figure out whether I am
missing anything.

C-style casts are syntactically different from the new forms, but
semantially they are equivalent to the static_cast, the const_cast,
the reinterpret_cast, or some combination thereof, or (sometimes)
to some cast that doesn't exist [legally] in C++. In this particular
case, it's static_cast. Plain and simple. AFAIK, of course.

>> to elaborate? And let's stick to technical C++ issues instead of
>> "perpetuating good habits" kind of argument. Thanks!


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Alan Woodland
Guest
Posts: n/a
 
      10-02-2007
James Kanze wrote:
>> char c = '1';
>> cout << '0x' << hex << c+0;

>
>> should do the trick.

>
>> "+0" causes a conversion to int.

>
> Shades of Fortran. If you want a conversion to int, why don't
> you say so:
>
> std::cout << hex << (int)c ;

C-Style casts?

surely

static_cast<int>(c);

or

int(c);

would better still?

Alan
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      10-02-2007
On 2007-10-02 04:52:23 -1000, "Victor Bazarov" <(E-Mail Removed)> said:

> Alan Woodland wrote:
>> James Kanze wrote:
>>>> char c = '1';
>>>> cout << '0x' << hex << c+0;
>>>
>>>> should do the trick.
>>>
>>>> "+0" causes a conversion to int.
>>>
>>> Shades of Fortran. If you want a conversion to int, why don't
>>> you say so:
>>>
>>> std::cout << hex << (int)c ;

>> C-Style casts?
>>
>> surely
>>
>> static_cast<int>(c);
>>
>> or
>>
>> int(c);
>>
>> would better still?

>
> I wonder why *in this particular case* it would be better. Care
> to elaborate? And let's stick to technical C++ issues instead of
> "perpetuating good habits" kind of argument. Thanks!
>


Everybody knows that C-style casts are evil. It's not about
perpetuating good habits, but about avoiding sin. Don't you care about
the state of your soul? Do you want to burn in hell for all eternity?

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-02-2007
Pete Becker wrote:
> On 2007-10-02 04:52:23 -1000, "Victor Bazarov"
> <(E-Mail Removed)> said:
>> Alan Woodland wrote:
>>> James Kanze wrote:
>>>>> char c = '1';
>>>>> cout << '0x' << hex << c+0;
>>>>
>>>>> should do the trick.
>>>>
>>>>> "+0" causes a conversion to int.
>>>>
>>>> Shades of Fortran. If you want a conversion to int, why don't
>>>> you say so:
>>>>
>>>> std::cout << hex << (int)c ;
>>> C-Style casts?
>>>
>>> surely
>>>
>>> static_cast<int>(c);
>>>
>>> or
>>>
>>> int(c);
>>>
>>> would better still?

>>
>> I wonder why *in this particular case* it would be better. Care
>> to elaborate? And let's stick to technical C++ issues instead of
>> "perpetuating good habits" kind of argument. Thanks!
>>

>
> Everybody knows that C-style casts are evil. It's not about
> perpetuating good habits, but about avoiding sin. Don't you care about
> the state of your soul? Do you want to burn in hell for all eternity?


LOL

Thanks, Pete! That was good.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments