Calculate checksum(hex) of string with ASCII(unsigned int). Use to detected communication error.
Embed:
(wiki syntax)
Show/hide line numbers
CheckSum.cpp
00001 #include "CheckSum.h" 00002 00003 00004 string CheckSum::calc(string str, int Byte) 00005 { 00006 // calculate SUM. 00007 int sum= 0; 00008 int size= str.size(); 00009 00010 for(int idx= 0; idx < size; idx++) 00011 sum += str[idx]; 00012 00013 // convert HEX. REWRITE/REUSE. 00014 str= I2A(sum, 16, (Byte* 2)); // Hex, over 2chars(ex. 0x0f). 00015 00016 // N[B] with the end of the hex checksum. 00017 size= str.size(); 00018 if(size > (Byte* 2)) // cut tail from str. 00019 return str.substr(size- (Byte* 2)); 00020 00021 if(size == (Byte* 2)) // return 00022 return str; 00023 00024 // size < Byte 00025 return "ERR"; 00026 } 00027 00028 bool CheckSum::compare(string str, string sum) 00029 { 00030 int Byte= sum.size()/ 2; 00031 return strCompareComplete(CheckSum::calc(str, Byte), sum); 00032 } 00033 00034 // EOF
Generated on Tue Jul 12 2022 23:25:41 by 1.7.2