Domanda

Hi I want to put each byte of an integer in a queue but for some reason I get a different value out.

int integer = 100;
char *chrp = (char *) &integer;
char charBuffer[10];
std::queue<char> queue;

for (int i = 0; i < sizeof(int); i++) {
    queue.push(chrp[i]);
}

for (int i = 0; i < queue.size(); i++) {
    charBuffer[i] = queue.front();
    queue.pop();
}

int *result = (int *) charBuffer;

I don't see why *result is not equal with integer. Thanks

È stato utile?

Soluzione

Each time round your second loop i goes up by one and queue.size() goes down by one - so you'll stop after reading half the data.

How about while (!queue.empty()) ?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top