-4

My code:

#include <iostream>

using namespace std;
int main() {
    unsigned int  a = 0x0009, b = 0x0002;
    unsigned int  c = a + b;
    cout << c;
}

Now

c = 11

I want to this:

c = 000B

How can I do ?

Pat. ANDRIA
  • 2,330
  • 1
  • 13
  • 27
yui
  • 19
  • 4
  • 2
    `cout << std::hex << c`? You won't get `000` unless you specify the width and supply a filler though. – Ted Lyngmo Nov 19 '20 at 10:29
  • 1
    check c++ `iomanip` – Harry Nov 19 '20 at 10:30
  • @TedLyngmo I don't want to use ```cout``` – yui Nov 19 '20 at 10:33
  • @Yksisarvinen No. Do you have without ```cout``` edition? – yui Nov 19 '20 at 10:34
  • 1
    @yui "_I don't want to use `cout`_" So, what is your question? The value of a number is the same, no matter with what number system you print it. – Algirdas Preidžius Nov 19 '20 at 10:35
  • What do you want to use then? [`std::printf()`](https://en.cppreference.com/w/cpp/io/c/fprintf)? – Yksisarvinen Nov 19 '20 at 10:35
  • 1
    Hex is just one possible representation of what's stored in `c`. This is starting to sound like an [XY problem](https://en.wikipedia.org/wiki/XY_problem). What is the real problem you've faced with the addition you performed? – Ted Lyngmo Nov 19 '20 at 10:36
  • @AlgirdasPreidžius Sorry. My English is not very good.. I want to stored in c, not ouput – yui Nov 19 '20 at 10:39
  • 2
    It _is_ stored in `c` already. You can choose to display it in any base you want. – Ted Lyngmo Nov 19 '20 at 10:40
  • 3
    @yui You already have that. The value stored in `c` is 11 in decimal system, or `0xB` in hexadecimal system, or `013` in octal system, or `0b1011` in binary. These are all the same value, just represented differently. – Yksisarvinen Nov 19 '20 at 10:40
  • 3
    Does [this](https://godbolt.org/z/Yqh75h) answer your question? – Ted Lyngmo Nov 19 '20 at 10:46
  • @yui.Your question is unclear, you want something else which is different from the example you provide. – Pat. ANDRIA Nov 19 '20 at 12:20

2 Answers2

3

When you do this

int main() {
    unsigned int  a = 0x0009, b = 0x0002;
    unsigned int  c = a + b;
}

Then c has the value of 11, it also has the value of 0x000B. It also has the value of 10 in a representation that uses 11 as base.

11 and 0x000B (and 10) are different representations of the same value.

When you use std::cout then the number is printed as decimal by default. What representation you choose to print the value on the screen has no influence whatsoever on the actual value of c.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • I don't want to stored in decimal. I want to stored in hexdecimal – yui Nov 19 '20 at 11:09
  • @yui It's most probably stored in binary format internally. What is the _real_ problem you're trying to solve? – Ted Lyngmo Nov 19 '20 at 11:10
  • @yui you cannot choose how it is stored internally. Why do you want that? – 463035818_is_not_an_ai Nov 19 '20 at 11:12
  • @idclev463035818 My thoughts because I have to keep counting in hexadecimal. – yui Nov 19 '20 at 11:19
  • @yui If I ask you the same question I've asked a couple of times already in a different way it may work. What makes you think it isn't stored in hexdecimal format? – Ted Lyngmo Nov 19 '20 at 11:21
  • 1
    @yui counting in hexadecimal is exactly the same as counting in decimal. You add `1` and the value increases by `1`. You add two numbers and the result is the sum of the two numbers. Why do you have to "keep counting in hexadecimal" ? – 463035818_is_not_an_ai Nov 19 '20 at 11:52
0

What I understand is that you want to retrieve the result in an hexadecimal specific format XXXX.

Computing the addition is the same as any number base, you only need to use (here I display) the result in your format.

You can do this, for instance:

#include <iostream>
#include <iomanip>


std::string displayInPersonalizedHexa(unsigned int a)
{
    std::stringstream ss;
    ss << std::uppercase<<  std::setfill('0') << std::setw(4) << std::hex<< a;
    std::string x;
    ss >>x;
    //std::cout << x;
    return x;
}
int main() {
    unsigned int  a = 0x0009, b = 0x0002;
    unsigned int  c = a + b;
    // displays 000B
    std::cout << displayInPersonalizedHexa(c) << std::endl;

    // adds c=c+1
    c=c+1;
    // displays 000C
    std::cout << displayInPersonalizedHexa(c) <<  std::endl;
    
   //0xC+5 = 0x11
    c=c+5;
    // displays 0011
    std::cout << displayInPersonalizedHexa(c) <<  std::endl;
}

This will output

000B
000C
0011
Pat. ANDRIA
  • 2,330
  • 1
  • 13
  • 27
  • Thanks. But I don't want to output c. I just want to store ```000B``` in ```c```, not ```11``` in ```c``` – yui Nov 19 '20 at 10:45
  • c is an unsigned int. you will have numbers in it. If you want to have `000B`, you should have another type (a string for instance) . I modified the code, now you can have a string variable containing `000B`. Does it suit? – Pat. ANDRIA Nov 19 '20 at 10:49