Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8153221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:56:49+00:00 2026-06-06T15:56:49+00:00

Every time I send a char to the cout object, it displays in ASCII

  • 0

Every time I send a char to the cout object, it displays in ASCII characters unless I cast it to an int.

Q: Is there a way to display the numerical value of a char without an explicit cast?

I read somewhere that doing too many casts in your code could lead to a loss of integrity (of your program). I am guessing that chars display in ASCII for a particular reason, but I’m not sure why.

I am essentially creating a game. I am using small numbers (unsigned chars) that I plan to display to the console. I may be paranoid, but I get this uneasy feeling whenever I spam static_cast<int> everywhere in my code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:56:50+00:00Added an answer on June 6, 2026 at 3:56 pm

    There is nothing wrong with type-casting, though, especially if you use static_cast to do it. That is what you should be using. It allows the compiler to validate the type-cast and make sure it is safe.

    To change the behavior of the << operator, you would have to override the default << operator for char values, eg:

    std::ostream& operator <<(std::ostream &os, char c)
    {
        os << static_cast<int>(c);
        return os;
    }
    
    char c = ...;
    std::cout << c;
    

    You could create a custom type that takes a char as input and then implement the << operator for that type, eg:

    struct CharToInt
    {
        int val;
        CharToInt(char c) : val(static_cast<int>(c)) {}
    };
    
    std::ostream& operator <<(std::ostream &os, const CharToInt &c)
    {
        os << c.val;
        return os;
    }
    
    char c = ...;
    std::cout << CharToInt(c);
    

    You could create a function that does something similar, then you don’t have to override the << operator, eg:

    int CharToInt(char c)
    {
        return c;
    }
    
    char c = ...;
    std::cout << CharToInt(c);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Every time i make a new JFrame object, the frame appears in the 0,0
My application should send a https GET request. Every time I get an exception
It seems a bug of Yahoo Weather API that every time I send a
In Mozilla FireFox, every time you send a request to the server that has
Say you've got an app that can send out POST requests every time an
Using this code, I can send emails to people. However, every time I press
every time i am writing some data to my server i first send a
I was using basic auth to send tweets from a server every time a
I can't get this script working. Every time I send a file the script
Every time i reset & seed my database it wipes out the standard admin@example.com

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.