1
char str[] = "helloworld"; // 10 characters + '\0'

char *s = str;
std::cout << (long int)s << std::endl;
std::cout << std::hex << (long int)s << std::endl;
// std::cout << std::dec;
// I found out that, using the above code reverts default formatting to decimal back.
std::cout << (long int)s << std::endl;

The output is:

140728139979229
7ffdd2cb19dd
7ffdd2cb19dd

Using std::hex once, does affect the formatting of other lines also, so I assume it is changing a setting. Now, my question is, how can I use it to affect only once, only the line it is used in?

If this is the only way it is used, then what can I use instead?

muyustan
  • 1,555
  • 1
  • 11
  • 23
  • use `static_cast(s)` instead of `(long int)s` – Ryan Haining Apr 06 '20 at 21:54
  • if you definitely need a way to convert a pointer to an integral type, `#include ` and use `reinterpret_cast(s)` – Ryan Haining Apr 06 '20 at 21:55
  • @RyanHaining actually, I am able to do it without `#include ` and by `uintptr_t(s)` or `(uintptr_t)s`. I think first one is due to the fact that I already include `` and it includes `` too, but I am not sure, if you know why please tell me. However, I don't have an opinion on why there is no need to use that long one starting with `reinterpret` in your comment. I can get the result by just `(uintptr_t)`. – muyustan Apr 06 '20 at 22:00
  • 2
    Just because it works doesn't mean it's a good idea. Standard library headers might include other headers, but that is not guaranteed by the standard so you should always include the headers you need yourself. See [this answer](https://stackoverflow.com/a/332086/9254539) for an explanation of the C++ casts. You should use C++ casts instead of C-style casts because C-style casts are dangerous and C++ casts are more readable. Don't sacrifice maintainability just to save a few seconds of typing. – eesiraed Apr 06 '20 at 22:15
  • @BessieTheCow thanks! – muyustan Apr 06 '20 at 22:18
  • tbh C-style casts are _way_ more readable than that long thing – Asteroids With Wings Apr 06 '20 at 22:19
  • @AsteroidsWithWings yeah but they're easy to misuse. You shouldn't be doing much casting anyway, but when you do, you want to know what type of cast it is rather than something that could be stripping const and volatile and converting to a potentially incompatible type – Ryan Haining Apr 06 '20 at 22:58
  • 1
    @muyustan totally agree with BessieTheCow – Ryan Haining Apr 06 '20 at 22:58
  • @RyanHaining You're not wrong – Asteroids With Wings Apr 07 '20 at 10:25

2 Answers2

3

Two ways, you can set it back using std::dec

std::cout << std::hex << (long int)s << std::dec << std::endl;

In general you can save the current state and then restore it

std::ios_base::fmtflags save = std::cout.flags();
std::cout << std::hex << (long int)s << std::endl;
std::cout.flags(save);

But this is tedious stuff.

john
  • 85,011
  • 4
  • 57
  • 81
  • yes, first way is pretty nice, thanks! I'll just wait in case someone provides a method even not requiring this one, otherwise I'll accept this one. – muyustan Apr 06 '20 at 21:54
  • @AsteroidsWithWings a stringstream? – Ryan Haining Apr 06 '20 at 21:57
  • Have a look at the [`std::setiosflags`](https://en.cppreference.com/w/cpp/io/manip/setiosflags) and [`std::resetiosflags`](https://en.cppreference.com/w/cpp/io/manip/resetiosflags) stream manipulators, instead of calling `cout.flags()` directly, eg: `std::cout << std::hex << (void*)s << std::resetiosflags(std::ios_base::hex) << std::endl;` – Remy Lebeau Apr 06 '20 at 22:00
  • @RemyLebeau Does that fix the thread safe problem brought up earlier in the comments? – Jerry Jeremiah Apr 06 '20 at 22:06
  • @JerryJeremiah no, it just makes restoring the old flags a little more streamlined. Any time there are two separate function calls (including `operator<<`), there is the potential for a task switch to another thread during or in between them. The only way to address the thread safety issue is with proper thread synchronization. – Remy Lebeau Apr 06 '20 at 22:14
  • @RyanHaining Heh @ I actually considered revealing that I sometimes use a local stringstream and dump `.str()` to `std::cout` in order to get around this problem, but decided not to because it's a horribly inefficient solution to not having a proper logging framework in place ;) (even one that just wraps all `cout` accesses) – Asteroids With Wings Apr 06 '20 at 22:18
1
char str[] = "helloworld"; // 10 characters + '\0'

char *s = str;
std::cout << (long int)s << std::endl;
std::cout << std::hex << (long int)s  << std::dec << std::endl;
std::cout << (long int)s << std::endl;

you want to change tou cout format back after you are done outputting in that format

...

so

cout << hex << varaible << dec << endl;  // this line makes the output hex then changes it back