How do I byteswap?

I have the following 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <iomanip>
#include <stdlib.h>

using namespace std;

unsigned long _byteswap_ulong(unsigned long value);

const size_t ROWS = 4;
const size_t COLS = 9;

int numberTable[ROWS][COLS] = 
{
	{1,2,3,4,5,6,7,8,9},
	{7,2,8,4,5,4,7,3,1},
	{9,9,7,4,9,9,8,7,6},
	{6,3,7,4,5,4,5,7,9}
};

int main(int argc, char* argv[])
{
	cout << setw(12) << left << "Byte Swap (Hex Version):" << endl;
	cout << setw(12) << left <<"Number" << setw(12) << "Before" << "After" << endl;
	cout << "-----------------------------------" << endl;

	long long Number_1_before_1= 123456789;
string Number_1 = “1”;
unsigned int 1= 123456789;
cout << showbase;
std::cout << std::hex << 1 << std::endl;
	
	long long Number_2_before_2= 728454731;
string Number_2 = “2”;
unsigned int 2= 728454731;
cout << showbase;
std::cout << std::hex << 2 << std::endl;

	long long Number_3_before_3= 997499876;
string Number_3 = “3”;
unsigned int 3= 997499876;
cout << showbase;
std::cout << std::hex << 3 << std::endl;

	long long Number_4_before_4= 637454579;
string Number_4 = “4”;
unsigned int 4= 637454579;
cout << showbase;
std::cout << std::hex << 4 << std::endl;
}

The first value 123456789, how do i byteswap this?
anyone help me?
If by 'byteswap' you mean performing a binary NOT on every byte, use the ~ operator:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main() {
    std::cout << std::hex;

    int x = 0x1E2D3C4B;
    std::cout <<  x << '\n';
    std::cout << ~x << '\n';

    int y = 123456789;
    std::cout <<  y << '\n';
    std::cout << ~y << '\n';

    return 0;
}
1e2d3c4b
e1d2c3b4
75bcd15
f8a432ea
Last edited on
Topic archived. No new replies allowed.