Recording Session


Packing

#include <iostream>
using namespace std;

int main() {

    unsigned int pack = 0x0;
    unsigned char elements[4] = { 0xAA ,0xBB , 0xCC , 0xDD };

    for (int var : elements) {
        pack <<= 8;
        pack |= var;
    }

    cout << hex << pack;

}

Un-Packing

#include <iostream>
using namespace std;

int main() {

    unsigned int pack = 0xAABBCCDD;
    unsigned char elements[4];
    unsigned int mask = 0x0;

    for (int i = 3; i >= 0; --i) {
        elements[i] = pack | mask;
        pack >>= 8;
    }

    for (int element: elements) {
        cout << hex << element << endl;
    }

}