# How to calculate checksum?

Status
Not open for further replies.

#### I14R10

##### Full Member level 3
Weather station is sending packets of 48 bits. Last 4 are checksum of all previous. For example, this is a real packet.

1001 0100 0100 1111 0101 1000 0100 0101 0100 1011 1010 and checksum is 1111

How is this checksum calculated?

Could be just about anything. Maybe just a running sum of all data. Maybe something totally different.

I would look at some other packets and see if my running sum theory is correct.

OK, so there is not only one way to calculate checksum, like: count of all "1"s in a signal?

Have you checked if it's just a simple checksum using a calculator before posting question?
Code:
#include <stdio.h>

#define N	12

const char *n[N] = {
"1001",
"0100",
"0100",
"1111",
"0101",
"1000",
"0100",
"0101",
"0100",
"1011",
"1010",
"1111"
};

unsigned tra(const char *s){//binary string to unsigned
unsigned x = 0;
while(*s){
x <<= 1;
if(*s == '1')
x |= 1;
s++;
}
return x;
}

void get(unsigned *v,const char **n,int k){
while(k-->0)
v[k] = tra(n[k]);
}

unsigned sum = 0;
while(n-->0)
sum += v[n];
return sum;
}

int main(void){
unsigned v[N];
get(v,n,N);
return 0;
}
Code:
result:
sum= 4f

It could be the last 4 bits of the checksum of the first 11 elements.
It seems ok for this packet.
Of course you need more packets to confirm it.

OK, so there is not only one way to calculate checksum, like: count of all "1"s in a signal?
There are many, many,many ways to calculate a checksum. The documentation for your device should tell you what method it uses.

Status
Not open for further replies.