I have this code C code that compiles OK on my laptop
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
unsigned int crc32(const void *m, size_t len) {
const unsigned char *message = m;
size_t i;
int j;
unsigned int byte, crc, mask;
i = 0;
crc = 0xFFFFFFFF;
while (i < len) {
byte = message[i];
crc = crc ^ byte;
for (j = 7; j >= 0; j--) {
mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
i = i + 1;
}
return ~crc;
}
int main() {
char str[] =
"620004";
size_t len = strlen(str);
unsigned int crc = crc32(str, len);
size_t len2 = (len + 1) / 2;
unsigned char arr2[len2];
for (size_t i = 0; i < len; i += 2) {
arr2[i / 2] = strtoul((char[3]) {str[i], str[i + 1], '\0'}, 0, 16);
}
crc = crc32(arr2, len2);
printf("CRC: 0x%X\n", crc);
return 0;
}
And I'm trying to port it to Arduino
#include <CRC32.h>
void setup() {
Serial.begin(115200);
uint8_t byteBuffer[] = "620004";
size_t numBytes = sizeof(byteBuffer) - 1;
size_t len2 = (numBytes + 1) / 2;
unsigned char arr2[len2];
CRC32 crc;
for (size_t i = 0; i < numBytes; i += 2) {
arr2[i / 2] = strtoul((char[3]) {byteBuffer[i], byteBuffer[i + 1], '\0'}, 0, 16);
}
for (size_t i = 0; i < numBytes; i++){
crc.update(byteBuffer[i]);
}
uint32_t checksum = crc.finalize();
Serial.print("CRC: ");
Serial
But I'm getting a
Arduino: 1.8.9 (Linux), Board: "Arduino/Genuino Uno"
/home/nico/Arduino/crc32/crc32.ino: In function 'void setup()':
/home/nico/Arduino/crc32/crc32.ino:11:50: warning: narrowing conversion of 'byteBuffer[i]' from 'uint8_t {aka unsigned char}' to 'char' inside { } [-Wnarrowing]
arr2[i / 2] = strtoul((char[3]) {byteBuffer[i], byteBuffer[i + 1], '\0'}, 0, 16);
^
/home/nico/Arduino/crc32/crc32.ino:11:69: warning: narrowing conversion of 'byteBuffer[(i + 1u)]' from 'uint8_t {aka unsigned char}' to 'char' inside { } [-Wnarrowing]
arr2[i / 2] = strtoul((char[3]) {byteBuffer[i], byteBuffer[i + 1], '\0'}, 0, 16);
^
crc32:11:84: error: taking address of temporary array
arr2[i / 2] = strtoul((char[3]) {byteBuffer[i], byteBuffer[i + 1], '\0'}, 0, 16);
^
exit status 1
taking address of temporary array
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
And I'm not really understanding what's the issue here with the strtoul function and its different behavior on Arduino
(char[3]) {byteBuffer[i], byteBuffer[i + 1], '\0'}, it has nothing to do withstrtoul. Who knows how old is the compiler where it works.char str[] = "620004";anduint8_t byteBuffer[] = "620004";