how do I byteswap values?
Oct 28, 2015 at 2:36am UTC
Hello, I have my code here and trying to byte swap the value in before_1~4.
I also have done to make as hex number as well.
All I need to do is swap the before value and come out to be like
before 123456789 to after 987654321.
How do I byteswap this using byteswap function or bitwise operator.
Thank You
Here is my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
int main(int argc, char * argv[])
{
// Display Header
cout << setw(17) << left << "Byte Swap (Hex Version):" << endl;
cout << setw(17) << left << "Number" << setw(17) << left << "Before" << setw(17) << left << "After" << endl;
cout << "--------------------------------------------" << endl;
long Before_1 = 123456789;
long Before_2 = 728454731;
long Before_3 = 997499867;
long Before_4 = 637454579;
string Number_1 = "1" ;
string Number_2 = "2" ;
string Number_3 = "3" ;
string Number_4 = "4" ;
cout << showbase << hex;
cout << setw(17) << left << "1" << setw(17) << left << Before_1 << endl;
cout << setw(17) << left << "2" << setw(17) << left << Before_2 << endl;
cout << setw(17) << left << "3" << setw(17) << left << Before_3 << endl;
cout << setw(17) << left << "4" << setw(17) << left << Before_4 << endl;
}
Oct 28, 2015 at 8:59am UTC
Oct 28, 2015 at 11:07am UTC
a char has 8 bits, so not the same thing. use reverse iterators
Topic archived. No new replies allowed.