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 6602417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:52:28+00:00 2026-05-25T18:52:28+00:00

I have a problem which I do not understand. I add characters to a

  • 0

I have a problem which I do not understand. I add characters to a standard string. Whe I take them out the value printed is not what I expected.

int main (int argc, char *argv[])
{
    string x;
    unsigned char y = 0x89, z = 0x76;
    x += y;
    x += z;
    cout << hex << (int) x[0] << " " <<(int) x[1]<< endl;
}

The output:
ffffff89 76

What I expected:
89 76

Any ideas as what is happening here?
And how do I fix it?

  • 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-05-25T18:52:29+00:00Added an answer on May 25, 2026 at 6:52 pm

    You have to account for the fact that char may be signed. If you promote it to int directly, the signed value will be preserved. Rather, you first have to convert it to the unsigned type of the same width (i.e. unsigned char) to get the desired value, and then promote that value to an integer type to get the correct formatted printing.

    Putting it all together, you want something like this:

    std::cout << (int)(unsigned char)(x[0]);
    

    Or, using the C++-style cast:

    std::cout << static_cast<int>(static_cast<unsigned char>(x[0]))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem, which I do not seem to be able to solve...
I have a problem with a string in C++ which has several words in
I have a problem which requires me to parse several log files from a
I have a problem which I think is related to forward declarations, but perhaps
I have a very simple problem which requires a very quick and simple solution
I have a problem with an application which uses the same stored procedure over
I have a problem with a little .Net web application which uses the Amazon
I have a problem in my project with the .designer which as everyone know
The problem: I have a class which contains a template method execute which calls
Consider this problem: I have a program which should fetch (let's say) 100 records

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.