Saving long numbers into eeprom

I have been looking for a way to save signed long numbers into the arduino's eeprom.
My initial idea was to use simple math to break it into separate bytes and then feed them to eeprom one by one.

Although it will probably work, I don t know how efficient this would be.

Any suggestions on doing this in a more graceful way?

The interface to the EEPROM is byte-wide, so yes, you're going to have to break down the values.
Different ways of doing this include casting byte pointers to the address of the variable you want to write, or use a union.
Or, as you say, simple shift and mask.

Or use this, which does it for you: Arduino Playground - HomePage


Very nice

Thanks a lot guys

that is beyond me. it should obvious work, but because there are no comments in the code, i have no idea what is happening. i can code plain c, but this seems to be c++.

:frowning: pity.
Yes, it's C++, but there's an example there.
The idea of the template is that it'll work for any datatype.

another reason to start studying c++. this seems to be a good example. but for now i have to find another plain c way to write and read a int to eeprom.

Shifts and ANDs do it for me.
[edit]Also some ORs, oh yes, definitely some of those[/edit]

yea. solved it that way. i only had to do a INT to two BYTE.

void readeventsCounter(){
  hiByte =;
  loByte =;
  eventsCounter = (hiByte << 8)+loByte;

void writeeventsCounter(){
  loByte = byte(eventsCounter);
  hiByte = byte(eventsCounter >> 8);
  EEPROM.write(0, hiByte);
  EEPROM.write(1, loByte);

but i have’nt tested it yet. the reference is not clear about the functionality of the typecast to byte. i assume (i know, dangerous!) that it takes the lower byte of the int, but i have to find out in debugging.

got a nice quote from a friend of mine about c++:

“If C gives you enough rope to hang yourself, then C++ gives you enough rope to bind and gag your neighborhood, rig the sails on a small ship, and still have enough rope to hang yourself from the yardarm”

he is into cocoa now.

void writeeventsCounter(){
 EEPROM.write(0, eventsCounter >> 8);
 EEPROM.write(1, eventsCounter & 0xff);

Better not to think of it as an "int", rather think of it as a "short", or an "int16"


See avr-libc: <avr/eeprom.h>: EEPROM handling. This library has routines to read and write bytes, ints and longs – all in C, no C++.

This library is already part of the Arduino environment. Don’t forget to add the#include <avr/eeprom.h>to the top of your sketch.

I’ve used some of these routines, and they work well.



gogging avr/eeprom.h i found a nice tutorial in pdf:

to those like me who wish to read and write long integers to EEPROM without using pointers.

  // read double word from EEPROM, give starting address
  unsigned long EEPROM_readlong(int address) {
  //use word read function for reading upper part
  unsigned long dword = EEPROM_readint(address);
  //shift read word up
  dword = dword << 16;
  // read lower word from EEPROM and OR it into double word
  dword = dword | EEPROM_readint(address+2);
  return dword;

//write word to EEPROM
  void EEPROM_writeint(int address, int value) {
  EEPROM.write(address+1 ,lowByte(value));
  //write long integer into EEPROM
  void EEPROM_writelong(int address, unsigned long value) {
  //truncate upper part and write lower part into EEPROM
  EEPROM_writeint(address+2, word(value));
  //shift upper part down
  value = value >> 16;
  //truncate and write
  EEPROM_writeint(address, word(value));

unsigned int EEPROM_readint(int address) {
  unsigned int word = word(,;
  return word;