#!/usr/bin/perl -w sub checksum16 ($) { # calculates the checksum 16 of the given string argument my @bytes = unpack("C*", $_[0]); my $sum = 0; foreach(@bytes) { $sum += $_; $sum %= 2**16; } return $sum; } print checksum16 ("t"); print "\n";
#include <stdio.h> #include <math.h> #include <string.h> typedef const char CRC; CRC crc_16(unsigned char c) { int i,j,k = 0; for(i=0;i!=8;c>>=1,i++) { k += c; k %= 1<<16; } return k; } int main() { printf("%u",crc_16('t')); printf ("\n"); return 0; }
#!/usr/bin/perl -w sub checksum16 ($) { # calculates the checksum 16 # of the given string argument my @bytes = unpack("C*", $_[0]); my $sum = 0; foreach(@bytes) { $sum += $_; $sum %= 2**16; } return $sum; } print checksum16 ("toto"); print "\n";
#include <stdio.h> unsigned int crc_16(char* s) { unsigned int sum = 0; while (s && *s) { sum += *s; sum %= 2 << 15; ++s; } return sum; } int main(void) { printf("%u\n", crc_16("toto")); return 0; }
#include <cstdlib> #include <cstdio> #include <string> typedef const char CRC; CRC crc_16( char c) { int i,j,k = 0; for(i=0;i<sizeof(c);i++) { k += c; k %= 2<<16; c>>=1; } return k; } int main() { printf("%d",crc_16('t')); printf ("\n"); return 0; }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionje me chope un warning
gcc -W -Wall -ansi -pedantic test_sum2.c -o test
g++ -W -Wall -Wabi -pedantic test_sum2.cpp -o test
#include unsigned int crc_16(const std::string& s) { unsigned int sum = 0; const unsigned int size = s.size(); for (unsigned int i = 0; i < size; ++i) { sum += s[i]; sum %= 2 << 15; } return sum; } int main() { std::cout << crc_16("toto") << std::endl; return 0; }