Я пытаюсь выяснить, как добавить последовательные байты в блок данных, начиная с определенного места (sequenceOffset) до определенной длины (sequenceLength), вводя их в 16-разрядные целые числа со знаком (int16_t). Числа могут быть отрицательными и положительными. Я также не могу использовать любые массивы, только синтаксис указателя.
*blockAddress points to the first byte of the memory region
*blockLength is number of bytes in the memory region
* sequenceOffset is the offset of the first byte of the sequence that
* is to be summed
* sequenceLength is the number of bytes in the sequence, and
* sequenceLength > 0
*
* Returns: the sum of the int16_t values obtained from the given sequence;
* if the sequence contains an odd number of bytes, the final byte
* is ignored; return zero if there are no bytes to sum
int16_t sumSequence16(const uint8_t* const blockAddress, uint32_t blockLength,
uint32_t sequenceOffset, uint8_t sequenceLength){
uint16_t sum = 0;
const uint8_t* curr = blockAddress; // deref
uint16_t pointer = *(uint16_t*)curr; // typecast to int16
for (uint16_t i = 0; i< sequenceLength; i++){
sum = sequenceOffset + (pointer +i +1);
}// for
пример теста:
- Суммирующая последовательность из 8 байтов со смещением 113:
5D 5 C 4E 6E FA B3 5D 4 C
23645 28238 -19462 19549
Вы сказали, что сумма: -7412
Должно быть: -13566
Я не уверен, как обработать случай, когда я игнорирую последний байт, если последовательность содержит нечетное количество байтов.