Skip to content

Instantly share code, notes, and snippets.

@memesaregood1
Created August 25, 2023 10:58
Show Gist options
  • Select an option

  • Save memesaregood1/51262ea2c105bb87abea06fd235c7523 to your computer and use it in GitHub Desktop.

Select an option

Save memesaregood1/51262ea2c105bb87abea06fd235c7523 to your computer and use it in GitHub Desktop.
Navtelecom messages encoder
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
unsigned char xor_sum(unsigned char *buffer, unsigned int length) {
unsigned char temp_sum = 0;
while (length-- > 0) {
temp_sum ^= *buffer++;
}
return temp_sum;
}
int main() {
std::string userInput;
std::cout << "Enter your input: ";
std::getline(std::cin, userInput);
std::vector<unsigned char> buffer1;
int byteValue;
for (char &c : userInput) {
byteValue = int(c);
buffer1.push_back(static_cast<unsigned char>(byteValue));
}
// Construct the second buffer using a vector
std::vector<unsigned char> buffer2 = {0x40, 0x4e, 0x54, 0x43, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, static_cast<unsigned char>(buffer1.size()), 0x00};
// Calculate the XOR sum of the message data
unsigned char xorResult = xor_sum(buffer1.data(), buffer1.size()); // checksum
buffer2.push_back(xorResult);
// Calculate the XOR sum of the header
unsigned char xorResult2 = xor_sum(buffer2.data(), buffer2.size());
buffer2.push_back(xorResult2);
// Construct the final message by concatenating buffer2 and buffer1
std::vector<unsigned char> finalBuffer = buffer2;
finalBuffer.insert(finalBuffer.end(), buffer1.begin(), buffer1.end());
// Print the final buffer
std::cout << "Final Buffer: ";
for (const auto &byte : finalBuffer) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte) << "";
}
std::cout << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment