{"id":343,"date":"2024-09-25T21:21:20","date_gmt":"2024-09-25T13:21:20","guid":{"rendered":"https:\/\/chaixiangyu.cn\/?p=343"},"modified":"2024-09-25T21:27:08","modified_gmt":"2024-09-25T13:27:08","slug":"%e9%80%9a%e7%94%a8%e5%bc%82%e6%88%96%e8%ae%a1%e7%ae%97%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/chaixiangyu.cn\/?p=343","title":{"rendered":"A function for XOR calculation"},"content":{"rendered":"\n<p>Here&#8217;s a function for XOR calculation that takes a <code>QByteArray<\/code> or any byte sequence as input and performs an XOR operation on all bytes, returning the final XOR result.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>\/\/ XOR calculation function, accepts QByteArray and returns a single-byte XOR result\nchar SerialWorker::calculateXOR(QByteArray arr_data) \n{\n    if (arr_data.isEmpty()) {\n        \/\/ If the array is empty, return 0 or handle the error as needed\n        return 0;\n    }\n\n    \/\/ Initialize XOR result with the first byte\n    char xor_result = arr_data.at(0);\n\n    \/\/ Perform XOR operation on each byte, starting from the second byte\n    for (int i = 1; i &lt; arr_data.size(); i++) {\n        xor_result ^= arr_data.at(i);\n    }\n\n    \/\/ Return the final XOR result\n    return xor_result;\n}\n<\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Code Explanation<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Boundary Check<\/strong>: The function first checks if the input array is empty to avoid performing operations on an empty array.<\/li>\n\n\n\n<li><strong>XOR Initialization<\/strong>: The XOR result is initialized with the first byte of the array.<\/li>\n\n\n\n<li><strong>Loop XOR<\/strong>: From the second byte onward, each byte is XORed with the current <code>xor_result<\/code>.<\/li>\n\n\n\n<li><strong>Return Result<\/strong>: The function returns the final XOR result.<\/li>\n<\/ol>\n\n\n\n<p>This function can be integrated into other logic for checksum or data processing tasks as needed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a function for XOR calculation that takes  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-343","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=\/wp\/v2\/posts\/343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=343"}],"version-history":[{"count":2,"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=\/wp\/v2\/posts\/343\/revisions"}],"predecessor-version":[{"id":349,"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=\/wp\/v2\/posts\/343\/revisions\/349"}],"wp:attachment":[{"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}