We want to use an integer as the key. This gives us the greatest possible range for key values. Unfortunately, integers are stored in 16 bit locations and the EEPROM storage is only 8 bits wide. When you use integers in BoostC the compiler generates code which manipulates them as two 8 bit values, now we are going to have to do the same to store and retrieve our key.

An eight bit location can hold 256 different patterns. A sixteen bit location can hold 256 * 256 possible patterns, giving a numeric range of 0 - 65,535. If you think about it the high byte gives the number of "256s" in the value and the low byte gives the remainder of the value. This means that we can split our number into two individual bytes by using DIV and MOD as follows:

low = number % 256
high = number / 256

I can now store these two eight bit values in my EEPROM and re-combine them later.

number = (high * 256) + low

The program on the right adds two new functions which will save and load integers. Remember that because an integer requires two locations you should not store another integer at "address + 1".

If you load up program Exercise 7.2 you ill find a version of my test program which works with integers. Note how I now add a much bigger number each time, so that I can make sure that both the high and low bytes in the value are being stored and retrieved correctly.