문제

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

도움이 되었습니까?

해결책

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()) ?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top