Reverse bytes in the integer
Jul 21, 2013 at 8:50pm UTC
Hi everybody. I wrote down some program to reverse bytes in integer through the char pointer, but it gives me trash. I can't handle it by myself. Please help.
It should give 00100110, but it gives 1677721600
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <iostream>
int main()
{
int number = 100;
char *buffer = (char *) &number;
char *reversedBuffer = (char *) new int ;
for (int i = 0; i < sizeof (number); i ++)
{
reversedBuffer[sizeof (number) - i - 1] = buffer[i];
}
int *reversedNumber = (int *) reversedBuffer;
std::cout << *reversedNumber << std::endl;
return 0;
}
Jul 21, 2013 at 9:04pm UTC
Investigate this code
1 2 3 4 5 6 7 8 9
#include <iostream>
int main()
{
int x = 0x64;
int y = 0x64000000;
std::cout << "x = " << x << ", y = " << y << std::endl;
}
The output is
Jul 21, 2013 at 9:07pm UTC
Thanks man, I will handle it.
Topic archived. No new replies allowed.