unsigned int to 2 bytes

I'm working in a project that mostly uses unsigned int variables (2 bytes).

With xbee i can only send bytes
If i want to store some variable in eeprom, i can only store bytes

how can I change from/to unsigned int to/from 2 bytes?

thank you


#include <EEPROM.h>

template <class T> int EEPROM_writeAnything(int ee, const T& value)
    const byte* p = (const byte*)(const void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
        EEPROM.write(ee++, *p++);
    return i;

template <class T> int EEPROM_readAnything(int ee, T& value)
    byte* p = (byte*)(void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
        *p++ = EEPROM.read(ee++);
    return i;

Is this code already built in 0018? or is needed to define that functions in my sketch?

Other question is…

unsigned int myvariable = 579;
unsigned int my2variable = 3355;
EEPROM_writeAnything(0, myvariable);
EEPROM_writeAnything(2, my2variable);

is this code right? if i save a unsigned int in byte 0, i will use byte 0 and 1. In the next operation i must use byte 2 and 3

and the last question: (by now)

struct config_t
    long alarm;
    int mode;
} configuration;
EEPROM_writeAnything(0, configuration);

will the memory map looks like this?
byte 0: 1st 1/4 alarm
byte 1: 2nd 1/4 alarm
byte 2: 3rd 1/4 alarm
byte 3: 4th 1/4 alarm
byte 4: 1st 1/2 mode
byte 5: 2th 1/2 mode


You might want to consider using the built in functions the Arduino has for extracting the low and high bytes.


thank you very much. very nice functions but... what about 4-bytes types?