Getting Weird characters back when reading EEPROM

Hi Guys,

I'm quite new to Aduino, I'm currently writing a sketch to write data and read it back from the EEPROM.
The issue I'm currently facing is the following:

I have written the OK word at address 30 in the EEPROM using EEPROM_writeAnything:

EEPROM_writeAnything(30, "OK");

but When I'm trying to read this block from the EEPROM using the following code I'm getting weird characters at the end of my OK:

char test[2];

eeprom_read_block((void*)&test, (const void*)30, 2);


This gives me the following in the Serial output instead of just giving me "OK":


But when I read this in HEX, I get the correct values:

4F and 4B

Anyone has any idea of what is going on here ?


You could make the receiving buffer long enought and read the closing '\0'

char test[3];

eeprom_read_block((void*)&test, (const void*)30, 3);


Nice, thanks for that, I thought I had tried it.

But what does this mean ?
That it's actually 3 Bytes and not 2 ?
Is there a way to remove the \0 ad only use 2 Bytes ?

Got and other question too, when I compare the collected in a if it just doesn't work:

if (test != "OK"){

 Serial.println("not configured: ");

    else if (test == "OK"){

      Serial.print("configured: ");

that will give me "not configured" instead of configured


In the EEPROM it is 3 bytes (because you wrote 3 bytes).

You already know how to get back two bytes, your original program did that
(the strange characters are the characters behind your array, that does not contain the trailing 0,
so println printed something unexpected (until hitting a 0).

You do not need the trailing 0 if you keep track of the individual lengths of strings, but that is rarely worth the trouble.

The comparison you used compares the pointers to the strings, not their content, use strcncmp or a similar function.

if (!strncmp(test, "OK", 2)) {


Thanks for the hint

So how do I have to proceed to keep out the end of the array from being written in there ?
I'll write and get the data manually anyway, I don't need the extra byte.

You will have to store the size somewhere if it is not implicit (signaled by the zero).

Probably there ist a eeprom_write_block routine you could call.

I used:

EEPROM_writeAnything(30, "OK");

to write in the EEPROM at first, and this function uses:


Here is the actual code for this:

template <class T> int EEPROM_writeAnything(int ee, const T& value)
    const byte* p = (const byte*)(const void*)&value;
    unsigned int i;
    for (i = 0; i < sizeof(value); i++){
  const byte b = *p;
  if( eeprom_read_byte( ( uint8_t* ) ee ) != b )
    eeprom_write_byte( ( uint8_t* ) ee++, b ), ++p;
    ee++, p++;
} // Only write the data if it is different to what's there
    return i;

So I should use EEPROM_write_block instead then ? like so:

EEPROM_write_block(30, "OK",2)