0

I have a 64 bit unsigned integer and for some reason I have to store it inside a string. What I am wondering is that the value inside the string is same even after using the swapped integer?

For example:

#include <iostream>
#include <byteswap.h>

using namespace std;

int main()
{
    uint64_t foo = 98;

    uint64_t foo_reversed = bswap_64(foo);
    
    std::string out = "";
    out.append(reinterpret_cast<const char*>(&foo), sizeof(foo));

    std::string out_reversed = "";
    out_reversed.append(reinterpret_cast<const char*>(&foo_reversed), sizeof(foo_reversed));
    
    std::cout << "out: " << out << std::endl;
    std::cout << "out_reversed: " << out_reversed << std::endl;

    return 0;
}

The string out and out_reversed have the exact same value, but I expect it to be different as the underlying integer foo and foo_reversed are swapped value of each other.

What am I missing here? Pardon me if it is a trivial mistake, but putting it out here on the chance that I'm missing some concept.

The output I see:

out: b
out_reversed: b

I was not expecting the above value for out_reversed

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
DonBaka
  • 325
  • 2
  • 14
  • 3
    What do you expect the output to show and what do you see? Note that just because two strings look the same when printed does not mean that they are the same. – user17732522 Mar 22 '22 at 13:12
  • 2
    Some questions: what is the byte string for decimal `98`? For each byte in there, what do you expect the ASCII representation to be? https://www.asciitable.com/ – JohnFilleau Mar 22 '22 at 13:15
  • A bad case of undefined behaviour. Anything can happen. Any expectation that you might have is totally unjustified. "Undefined behaviour" means it will only crash when it's important. – gnasher729 Mar 22 '22 at 13:21
  • 2
    @gnasher729 no UB here – Caleth Mar 22 '22 at 13:22
  • 2
    @gnasher729: Where is the undefined behaviour? Note the length argument passed to `append`. And `reinterpret_cast` to a `const char*` is allowed. – Bathsheba Mar 22 '22 at 13:33

1 Answers1

4

You can see the same thing with arrays of char:

int main()
{
    char foo[8] = { 98, 0, 0, 0, 0, 0, 0, 0 };
    char foo_reversed[8] = { 0, 0, 0, 0, 0, 0, 0, 98 };
    
    std::string out(foo, 8);
    std::string out_reversed(foo_reversed, 8);

    std::cout << "out: " << out << std::endl;
    std::cout << "out_reversed: " << out_reversed << std::endl;

    return 0;
}

The chars with value 0 aren't printable, so the terminal doesn't display them

Here's an alternative printing.

Caleth
  • 52,200
  • 2
  • 44
  • 75
  • Ah, my bad. But they have to show something, let's say null, or empty space, or any other representation? – DonBaka Mar 22 '22 at 13:21
  • 2
    @DonBaka That's up to the terminal, not the C++ program. E.g. "displaying" a char with the value 7 might *make a noise* – Caleth Mar 22 '22 at 13:22
  • 1
    @DonBaka A terminal can do what it wants with non-printable characters. Some don't support the "bell" character, many don't support vertical tab, the handling of horizontal tabs is a constant source of confusion for people who never used a mechanical typewriter, and there's a whole bunch of teletype-era characters that just don't make sense on a computer screen. – molbdnilo Mar 22 '22 at 13:25