Is there a simple way to concatenate two numbers (A+B = AB)?

I have a temperature and a humidity value that I'm saving to EEPROM. I had been saving each to a separate address location, but in order to fit more in EEPROM, I wanted to concatenate the numbers.


[u]Old way:[/u] Temperature = 73 Humidity = 26 I'd store 73 in address 0, and 26 in address 1.

[u]New way (if I can):[/u] Temp = 73 Humidity = 26 I'll store both in address 0 by saving "73.26" or something similar. Then I can fit more in EEPROM!

The values for temperature may range from -100 to +150 and the values for humidity may range from 0 to 100.

EEPROM is only 8 bits wide - you need 4 bits for each digit. Thus 73.26 is still going to need 16 bits, or 2 bytes.

Are you treating the temperature as 0 to 255, with 0 representing -100 and 255 representing +155?

The values for temperature may range from -100 to +150 and the values for humidity may range from 0 to 100.

No way to do that in < 2 bytes.

Also how often are you writing this value? EEPROM has a limit on the number of writes (although it’s pretty high, 100k I think).


It is perfectly reasonable to store the two items of data and consume just one EEPROM address for each.

RH fits nicely if you store it as a byte integer. The least significant (0-255) byte of any integer will store at one EEPROM address.

Temperature could have been tricky, but by happy chance you have decided that the range of values will be less than 256. Temperature will fit nicely in one byte if you are clever. Clever in this instance means add 100 to the byte integer value of temperature when you save it and subtract 100 each time you pull it out and use it. This procedure will cause every value stored in EEPROM to be positive number.

Let us know us how it works out.

You could save half as often to achieve the same effect. Probably better still, do run-length encoding. You didn't say how often you save, but unless it is hours apart, the temperature and humidity are likely to be often the same. So you could do something like this...

Save: Count, temperature, humidity (3 bytes) whenever the temperature or humidity changes.

For example:

4 x temp=21 humidity=40   (ie. 4, 21, 40)
8 x temp=22 humdity=41   (ie. 8, 22, 41)

Since a single byte will hold 256 values that will hold the temperature (-100 to +150 is only 250 values) and the humidity percent (0 to 100).

If you were previously measuring (say) every minute, you might find a hundred readings overnight where it doesn't change. Of course, you need to detect if you get 255 identical readings and output a new one anyway. This method could save you hundreds of bytes, per day.

The other approach is to add in an external EEPROM. I did a write-up recently about a 32kB (256 kBits) EEPROM which is only in a DIP8 package which you talk to using I2C. Since you can do a million erase/program cycles anyway, and you will take a while to fill 32Kb, this should last quite a while. And when it starts wearing out, pull it out of the chip holder and replace it for $5 or whatever it costs. In fact I calculate that at the rate of writing 2 bytes (temp/humidity) per second, the 32Kb should last 519 years.


Bytes in chip: 32 * 1024 = 32768
A million rewrite cycles: 32,768,000,000
Using 2 bytes per second: 16,384,000,000
Divide that by 60 * 60 * 24 * 365 = 519.533

So if you are still around in 519 years, then you replace the chip!

For a description of doing this with I2C: