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

  • Home
  • SEARCH
  • 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 198747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:57:51+00:00 2026-05-11T16:57:51+00:00

In C++, to print a number in hexadecimal you do this: int num =

  • 0

In C++, to print a number in hexadecimal you do this:

int num = 10;
std::cout << std::hex << num; // => 'a'

I know I can create a manipulator that just adds stuff to the stream like so:

std::ostream& windows_feed(std::ostream& out)
{
    out << "\r\n";
    return out;
}

std::cout << "Hello" << windows_feed; // => "Hello\r\n"

However, how can I create a manipulator that, like ‘hex’, modifies items to come on the stream? As a simple example, how would I create the plusone manipulator here?:

int num2 = 1;
std::cout << "1 + 1 = " << plusone << num2; // => "1 + 1 = 2"

// note that the value stored in num2 does not change, just its display above.
std::cout << num2; // => "1"
  • 1 1 Answer
  • 2 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-11T16:57:51+00:00Added an answer on May 11, 2026 at 4:57 pm

    First, you have to store some state into each stream. You can do that with the function iword and an index you pass to it, given by xalloc:

    inline int geti() { 
        static int i = ios_base::xalloc();
        return i;
    }
    
    ostream& add_one(ostream& os) { os.iword(geti()) = 1; return os; } 
    ostream& add_none(ostream& os) { os.iword(geti()) = 0; return os; }
    

    Having that in place, you can already retrieve some state in all streams. Now, you just have to hook into the respective output operation. Numeric output is done by a facet, because it potentially is locale dependent. So you can do

    struct my_num_put : num_put<char> {
        iter_type 
        do_put(iter_type s, ios_base& f, char_type fill, long v) const { 
            return num_put<char>::do_put(s, f, fill, v + f.iword(geti())); 
        } 
    
        iter_type 
        do_put(iter_type s, ios_base& f, char_type fill, unsigned long v) const { 
            return num_put<char>::do_put(s, f, fill, v + f.iword(geti())); 
        } 
    }; 
    

    Now, you can test the stuff.

    int main() {
        // outputs: 11121011
        cout.imbue(locale(locale(),new my_num_put));
        cout << add_one << 10 << 11 
             << add_none << 10 << 11;
    }
    

    If you want that only the next number is incremented, just set the word to 0 again after each call to do_put.

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

Sidebar

Related Questions

I using this loop to print number DECLARE @A Int SET @A = 33
How can I set a digit in a hexadecimal number? I currently have this
How can I represent a given floating point number in hexadecimal form? For example,
for number in range(1,101): print number Can someone please explain to me why the
How can I print number in any component like JLabel with certain number of
Hi I am creating a method that will take a number and print it
I have this code here: setcookie('visitcount',1+$_COOKIE['visitcount'],time()+60*60); $visitcount = $_COOKIE['visitcount']; print Number of views: .$visitcount;
When printing a reference to an array, hash, etc, what is that hexadecimal number
? MAKE num RANDOM 1 ? IF [:num = 1] [print Number is one.]
I was just getting familiar with sleep(), i found that #include<stdio.h> int main() {

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.