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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:02:50+00:00 2026-05-16T00:02:50+00:00

I am a beginner with C++. I have a new project at work where

  • 0

I am a beginner with C++. I have a new project at work where I have to learn it, so I’m trying some things just to test my understanding. For this problem, I’m trying to read a file and then print it on screen. Super simple, just trying to get good at it and understand the functions that I’m using. I copied some text from a MS Word document into a notepad (*.txt) file, and I’m trying to read this *.txt file. All of the text in the word document is bolded, but other than that there are no ‘unusual’ characters. Everything prints out on the screen as it appears in the document except the bolded ” – ” symbol. This character is printed as the “u” with a hat character (“so called extended ASCII” code 150). I try to print out the integer value of this character in my array (which should be 150) but I get -106. I realize this signed integer has the same bits as the unsigned integer 150. My question is how to get the output to say 150? Here’s my code:

        #include <iostream>
        #include <fstream>
        using namespace std;

       int main() {
       unsigned char* input1;
       int input1size = 57;
       ifstream file("hello_world2.txt",ios::binary | ios::ate);
       if (file.is_open()){
            int size;
            size = (int) file.tellg();
            cout <<"This file is " << size << " bytes." << endl;
            file.seekg(0,ios::beg);
            input1 = new unsigned char[input1size];
            file.read(input1, input1size);
            cout << "The first " << input1size <<" characters of this file are:" << endl<<endl;
            for (int i=0; i<input1size; i++) {
            cout << input1[i];
       }
       cout<<endl;
       }
       else {
       cout <<"Unable to open file" << endl;
       int paus;
       cin>>paus;
       return 0;
       }
       file.close();
       int charcheck = 25;
       int a=0;
       int a1=0;
       int a2=0;
       unsigned int a3=0;
       unsigned short int a4=0;
       short int a5=0;
       a = input1[charcheck];
       a1 = input1[charcheck-1];
       a2 = input1[charcheck+1];
       a3 = input1[charcheck];
       a4 = input1[charcheck];
       a5 = input1[charcheck];
       cout <<endl<<"ASCII code for char in input1[" << charcheck-1 <<"] is: " << a1 << endl;
       cout <<endl<<"ASCII code for char in input1[" << charcheck <<"] is: " << a << endl;
       cout <<endl<<"ASCII code for char in input1[" << charcheck+1 <<"] is: " << a2 << endl;
       cout <<endl<<"ASCII code for char in input1[" << charcheck <<"] as unsigned int: " << a3 << endl;
       cout <<endl<<"ASCII code for char in input1[" << charcheck <<"] as unsigned short int: " << a4 << endl;
       cout <<endl<<"ASCII code for char in input1[" << charcheck <<"] as short int: " << a5 << endl;
       int paus;
       cin>>paus;
       return 0;
       }

Output for all this looks like:

    This file is 80 bytes.
    The first 57 characters of this file are:

    STATUS REPORT
    PERIOD 01 u 31 JUL 09

    TASK 310: APPLIC

    ASCII code for char in input1[24] is: 32
    ASCII code for char in input1[25] is: -106
    ASCII code for char in input1[26] is: 32
    ASCII code for char in input1[25] as unsigned int: 4294967190
    ASCII code for char in input1[25] as unsigned short int: 65430
    ASCII code for char in input1[25] as short int: -106

So it appears “int a” is always read as signed. When I try to make “a” unsigned, it turns all the bits left of the eight bits for the char to 1’s. Why is this? Sorry for the length of the question, just trying to be detailed. Thanks!

  • 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-16T00:02:50+00:00Added an answer on May 16, 2026 at 12:02 am

    What you’re dealing with is the sign-extension that takes place when the char is promoted to int when you assign it to one of your a? variables.

    All the higher order bits must be set to 1 to keep it the same negative value as was in the smaller storage of the char.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Super-beginner easy points ruby question. I'm trying to learn some ruby by programming the
I am a beginner with WPF and trying a home project to become familiar
I am a beginner of python and have a question, very confusing for me.
I have written a program with both a Advanced Mode and a Beginner Mode
I'm an absolute beginner, you see. Say I have a string object on the
A beginner question, bear with me: I'm just wondering under what circumstances one should
I'm not a beginner at C# but I really need to increase my understanding,
I am a complete beginner trying to develop for FCKeditor so please bear with
As a complete beginner with no programming experience, I am trying to find beautiful
I know that this is quite subjective, but is it something that I have

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.