1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
uint8 normalChecksum8(uint8 *b, int n)
{
int i;
uint16 a, bb;
//Sums bytes 1 to n-1 unsigned to a 2 byte value. Sums quotient and
//remainder of 256 division. Again, sums quotient and remainder of
//256 division.
for(i = 1, a = 0; i < n; i++)
a+=(uint16)b[i];
bb = a / 256;
a = (a - 256 * bb) + bb;
bb = a / 256;
return (uint8)((a-256*bb)+bb);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$data[1] = "\xA3";
$data[2] = "\x05";
$data[3] = "\x00";
$data[4] = "\x00";
$data[5] = "\x00";
$data[6] = "\x00";
$data[7] = "\x00";
$data[0] = normalChecksum8(@data);
####################################################################
sub normalChecksum8
{
my $a;
my $b;
foreach (@_)
{ $a += pack("C",$_); }
$b = $a / 256;
$b = $a / 256;
$a = ($a - 256 * $b) + $b;
$b = $a / 256;
return pack("C", (($a-256*$b)+$b));
}
$a += pack("C",$_);
1 2 3 4 5 6 7 8 9 10 11 12 13
use 5.14.0; my @data = map { unpack "C", $_ } ("\xA3", "\x05", "\x00", "\x00", "\x00", "\x00", "\x00"); sub checksum { use integer; my $sum16bit = 0; $sum16bit = ($sum16bit + $_) & 65535 for @_; my $sumdivmod = ($sum16bit >> 8) + ($sum16bit & 255); return ((($sumdivmod >> 8) + ($sumdivmod & 255)) & 255); } say checksum(@data);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
use integer; my @uha = qw(1 2 3 4 2 4 6 0 12); # Uint8 Array say normalChecksum(@uha, scalar @uha); # 33 say simpleChecksum(@uha); # 33 # von c nach Perl sub normalChecksum{ my $n = pop @_; my ($i,$a,$bb); for($i = 1, $a = 0; $i < $n; $i++){ $a += $_[$i]; } $bb = $a / 255; $a = ($a - 256 * $bb) + $bb; $bb = $a / 256; return ($a-256*$bb) + $bb; } # Vereinfachung mit pack sub simpleChecksum{ my @uha = @_; return unpack("%C*", pack "C*", @uha) - 1; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
// 1 2 3 4 2 4 6 0 12
int main(void){
int b[] = {1, 2, 3, 4, 2, 4, 6, 0, 12};
printf("%u\n", normalChecksum(b, 9));
return 0;
}
int normalChecksum(int *b, int n){
int i;
int a, bb;
for(i = 1, a = 0; i < n; i++)
a+=(int)b[i];
bb = a / 256;
a = (a - 256 * bb) + bb;
bb = a / 256;
return (int)((a-256*bb)+bb);
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#!/usr/bin/perl use warnings; use strict; use Inline C => <<'END_OF_C_CODE'; #include <stdint.h> SV* normalChecksum8(char *b, int n) { int i; uint16_t a, bb; //Sums bytes 1 to n-1 unsigned to a 2 byte value. Sums quotient and //remainder of 256 division. Again, sums quotient and remainder of //256 division. for(i = 1, a = 0; i < n; i++) a+=(uint16_t)b[i]; bb = a / 256; a = (a - 256 * bb) + bb; bb = a / 256; return newSVpvf("%d", (a-256*bb)+bb); } END_OF_C_CODE print normalChecksum8("3532",2). "\n";
return ( (double)((int)resultWh) + (double)(resultDec)/4294967296.0 );
print unpack('d',$resultWh) + unpack('d',$resultDec/4294967296.0);
1 2
perl -E '$resultWh=4294967295;$resultDec = 4238174830;say ((unpack"l",pack"l",$resultWh)+$resultDec/4294967296)' -0.0132230264134705