Small existing checksum function does not work on Arduino

I am trying to build a config storage system using the EEPROM on an ESP8266 based module.
Some years ago I had the same kind of task on a Microchip PIC system and I figured copying over the working function to Arduino would be OK. But apparently not.. >:(
This is my existing function from the PIC environment:

UINT CheckSum(void *block, UINT len)
{
    unsigned char * data;
    UINT chk=0;
    data = block;

    while(len--) chk += *data++;
    return chk;
}

In converting to Arduino I did a change of UINT to unsigned int and unsigned char to byte:

unsigned int CheckSum(void *block, unsigned int len)
{
    byte *data;
    unsigned int chk=0;
    data = block;

    while(len--) chk += *data++;
    return chk;
}

What happens is that I get a verify error in Arduino:

sketch\espconfig.cpp:43:10: error: invalid conversion from 'void*' to 'unsigned char*' [-fpermissive]
     data = block;
          ^

So the assignment is invalid here but has worked fine in earlier similar non-Arduino projects?
What can I do to make this work? I don't understand the error message fully...
All this function does is that it adds together len bytes starting at beginning of block into an unsigned int which is then returned.
It was written like this in order to be used for a number of items of varying types like this (in this example EEPROM is a struct type in the PIC program):

void UpdateChecksum(EEPROM * data)
{
    data->checksum = 0;
    data->checksum = CheckSum(data, sizeof(EEPROM));
}

Any ideas, please?

try:

data = (byte*) block;

Changed the type of block in the function header from void to byte.
This forces the user of the function to cast the datatype of block to byte
and optionally adjust the parameter len to match the length in bytes.

I did this test and it works on an UNO with IDE 1.8.5

byte table[5] = { 1, 2, 3, 4, 5 };

void setup() 
{
  Serial.begin(115200);
  Serial.println(__FILE__);

  uint16_t chk = CheckSum(table, 5);

  Serial.println(chk);
}

void loop()
{}

unsigned int CheckSum(byte *block, unsigned int len)
{
    byte *data;
    unsigned int chk = 0;
    data = block;

    while(len--) chk += *data++;
    return chk;
}

arduino_new:
try:

data = (byte*) block;

Thanks a million! :slight_smile: :slight_smile: :slight_smile:
This made the sketch compile successfully! Now I “just” need to check that it actually also works as I meant it to…

robtillaart:
Changed the type of block in the function header from void to byte.
This forces the user of the function to cast the datatype of block to byte
and optionally adjust the parameter len to match the length in bytes.

byte table[5] = { 1, 2, 3, 4, 5 };

void setup()
{
 Serial.begin(115200);
 Serial.println(FILE);

uint16_t chk = CheckSum(table, 5);

Serial.println(chk);
}

void loop()
{}

unsigned int CheckSum(byte *block, unsigned int len)
{
   byte *data;
   unsigned int chk = 0;
   data = block;

while(len--) chk += *data++;
   return chk;
}

Well, then the CheckSum() function can no longer be used with any type of buffer. Using void it only needs the address to the data and a length, so it can be used everywhere a checksum is needed. That is why I wrote it like shown back in 2013. I could have used a specific data type if there is only one kind of type I need to checksum, but I like using generic portable functions in cases like this...

PS:
Whenever I try to reply to more than one post here or whenever I need to edit a post I did which contains mis-spellings and such I cannot post!
Instead I get a message like this in my face:

The message has the following error or errors that must be corrected before continuing: You have exceeded the number of posts you can make in a 5 minutes period. Please try again later.

Why does this happen? Surely I should be able to reply to more than one other than one?

The 5 minute restriction goes away after 100 posts