Reputation: 4532
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
Upvotes: 0
Views: 454
Reputation: 18974
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())
?
Upvotes: 4