c++ - Parse unsigned char[] to std::string -
i have problem parsing contents of char[]. contains bytes, can formated ascii stings. last 2 bytes crc. therefore interpret last 2 entries in array in hex string:
std::ostringstream payload; std::ostringstream crc; payload << std::hex; crc << std::hex; // last 2 bytes crc (int = 0; < this->d_packetlen - 2; i++) { payload << static_cast<unsigned>(this->d_packet[i]); (int j = i; j < this->d_packetlen; i++) { crc << static_cast<unsigned>(this->d_packet[j]); } } std::string payload_result = payload.str(); std::string crc_result = crc.str(); fprintf(d_new_fp, "%s, %s, %d, %d\n", payload_result.c_str(), crc_result.c_str(), this->d_lqi, this->d_lqi_sample_count); this doesn't work, , i'm not sure why is? there easier way cast unsinged chars ascii?
best, marius
this infinite loop:
for (int j = i; j < this->d_packetlen; i++) { crc << static_cast<unsigned>(this->d_packet[j]); } in loop, not incrementing j; instead you're incrementing i. maybe, problem?
also, way you've described problem, think correct solution this:
for (int = 0; < this->d_packetlen - 2; i++) { payload << static_cast<unsigned int>(this->d_packet[i]); } (int j = this->d_packetlen - 2; j < this->d_packetlen; j++) { crc << static_cast<unsigned int>(this->d_packet[j]); } that second loop should outside first loop.
Comments
Post a Comment