How to show decimal value of a char using cout?

  • Thread starter Charles Kerekes
  • Start date
C

Charles Kerekes

Hello everyone,

I am still learning C++, so I'm not sure what I'm trying to do is
possible. Her is a simplified example:

char Test = 'L';
cout << "Test Char as decimal: " << dec << Test << endl;

In the above cout statement, I was hoping it would show 76, the
decimal value of the L character. Instead, it displays an L. Is there
a way to use cout to show the value?

I know I can accomplish this with printf, but wanted to see if cout
could also do it. Thanks in advance.

Charlie
 
U

Unforgiven

Charles Kerekes said:
Hello everyone,

I am still learning C++, so I'm not sure what I'm trying to do is
possible. Her is a simplified example:

char Test = 'L';
cout << "Test Char as decimal: " << dec << Test << endl;

cout << static_cast<int>(Test) << endl;
 
K

Kevin Goodsell

Charles said:
Hello everyone,

I am still learning C++, so I'm not sure what I'm trying to do is
possible. Her is a simplified example:

char Test = 'L';
cout << "Test Char as decimal: " << dec << Test << endl;

In the above cout statement, I was hoping it would show 76, the
decimal value of the L character. Instead, it displays an L. Is there
a way to use cout to show the value?

I know I can accomplish this with printf, but wanted to see if cout
could also do it. Thanks in advance.

Charlie

'dec' is the default, so you don't change anything by applying it
(unless you've previously applied one of its counterparts, hex or oct).
It only affects how integers are printed, and even though chars are
technically integral types, they aren't printed as numbers.

As the other replies suggested, convert it to an integer.

-Kevin
 
L

Leor Zolman

...or:

cout << (int) Test << endl;

...or even:

cout << Test + 0 << endl;

That made me think a little harder about what the style issues could be
here... the first response, using the static_cast, seemed fine to me.
Aren't new-style casts always supposed to be better than C-style casts?
"Fine," I said.

Then the pseudo-constructor response. "Yeah", I thought, "Why not? It isn't
really a 'cast', so it is Kosher-for-C++ without the ugly new-style cast
syntax! Cool."

Then your post came up. Are you trying to ridicule the second post because
the first was a "good enough" answer? Are you seriously saying that an
old-style cast is "better" (in some style sense) than either of the two
earlier options? That it is "as good as" they are because we know in this
case there's no danger in this specific cast? Or are you implying something
else altogether that I haven't picked up on yet?

So far, my favorite option is Jon's, and I appreciate his posting it
because I would not have thought of doing that.
-leor
 
R

Rob Williscroft

Leor Zolman wrote in
Then the pseudo-constructor response. "Yeah", I thought, "Why not? It
isn't really a 'cast', so it is Kosher-for-C++ without the ugly
new-style cast syntax! Cool."

The cast int( x ) is the same as (int)( x ), even for a UDT
(say class udt), udt( x ) might not invoke udt's constructor.

Contrived example:

struct A
{
A( int ) {}
};

struct B
{
operator A()
{
std::cerr << "operator A()\n";
return A( 0 );
}
};

void f( A ) {}

int main()
{
B b;
f( A( b ) );
}



Rob.
 
L

Leor Zolman

Leor Zolman wrote in

The cast int( x ) is the same as (int)( x ), even for a UDT
(say class udt), udt( x ) might not invoke udt's constructor.

Bad wording on my part. What I meant by "it isn't really a cast" was that
"it didn't use the old-style cast syntax; there may be pressure to rewrite
old-style casts as new-style casts, but there's no pressure to rewrite
pseudo-constructor-style casts because that's already a bona-fide modern
C++ style."
-leor
 
K

Kevin Goodsell

Leor said:
Bad wording on my part. What I meant by "it isn't really a cast" was that
"it didn't use the old-style cast syntax; there may be pressure to rewrite
old-style casts as new-style casts, but there's no pressure to rewrite
pseudo-constructor-style casts because that's already a bona-fide modern
C++ style."
-leor

I don't like function-style casts much. Unless I'm mistaken, they have
the same problems as C-style casts, plus you can't easily write them for
types that need a qualifier, e.g. unsigned int.

-Kevin
 
C

Charles Kerekes

Thanks for everyone's suggestions. I tried the cast method with the
cout statement, but had a problem when the value was over 127. Here is
what I think happened:

If the char value was under 128, the cast forced the single byte into
a short, padding all bits to the left with the leftmost bit of the
original byte, like this:

01111111 --> 00000000 01111111

But, when the char value was 128 or larger, the conversion to short
padded with 1's because that was the bit in the left-most position,
like this:

10000000 --> 11111111 10000000

So, the result was that char values of 128 or above gave a very large
short. To solve it, I forced the left bits of the short to zeros like
this:

cout << "Test Char as decimal: " << (short)(0x00FF & Test) << endl;

Charlie
 
K

Kevin Goodsell

Charles said:
Thanks for everyone's suggestions. I tried the cast method with the
cout statement, but had a problem when the value was over 127. Here is
what I think happened:

If the char value was under 128, the cast forced the single byte into
a short, padding all bits to the left with the leftmost bit of the
original byte, like this:

01111111 --> 00000000 01111111

But, when the char value was 128 or larger, the conversion to short
padded with 1's because that was the bit in the left-most position,
like this:

10000000 --> 11111111 10000000

So, the result was that char values of 128 or above gave a very large
short. To solve it, I forced the left bits of the short to zeros like
this:

cout << "Test Char as decimal: " << (short)(0x00FF & Test) << endl;

That's not the way it works. The value must be preserved if the
destination type can represent it. The most likely reason for what you
saw is that the char's value was /not/ over 127. After all, 127 is the
portable limit for type char. Odds are that chars can't contain values
larger than 127 on your system.

As for why you got a very large number... well, if you casted to short
that doesn't make much sense on typical systems. Are you sure you didn't
cast to an unsigned type?

Post a short sample program and the output you observe from it.

-Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top