'4 chars' to int to '4 chars': serialisation/de-serialisatio

If you have a simple question and just want an answer.
User avatar
Chendy
Active Member
Posts: 46
Joined: Tue Nov 02, 2010 4:53 pm

'4 chars' to int to '4 chars': serialisation/de-serialisatio

Post by Chendy »

I have seen that when using ports, XMOS/XC provides methods for serialising and deserialising data.

Is there any methods (or whats the best way) to going from 4 unsigned chars to an unsigned int, and back again...

... for general programming?

... for channel communication?

 

I know how to this with bitshifts + bitwise AND or OR, but was wondering if there were any better tricks/tips!

Thanks

Chendy



User avatar
Andy
Respected Member
Posts: 279
Joined: Fri Dec 11, 2009 1:34 pm

Post by Andy »

For channel comms, did you know you can send a structure over a channel? You could pack up your char array in an struct for example:

typedef struct foo {
    char c[4];
} foo;
 
void f(chanend c) {
    foo bar;
    // ... Initialise bar
    c <: bar;
}
 
void g(chanend c) {
    foo bar;
    c :> bar;
}
 
int main(void) {
    chan c;
    par {
        f(c);
        g(c);
    }
}