I have a problem with parsing the contents of a char[]. It contains bytes, that can be formated as ASCII stings. The last two bytes however are CRC. Therefore I interpret everything but the last two entries in the array in hex to string:
std::ostringstream payload;
std::ostringstream crc;
payload << std::hex;
crc << std::hex;
// last two bytes are CRC
for (int i = 0; i < this->d_packetlen - 2; i++)
{
payload << static_cast<unsigned>(this->d_packet[i]);
for (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, and I'm not sure why that is? Is there an easier way to cast unsinged chars to ASCII?
Best, Marius
This is an infinite loop:
for (int j = i; j < this->d_packetlen; i++)
{
crc << static_cast<unsigned>(this->d_packet[j]);
}
In this loop, you are NOT incrementing j
; instead you're incrementing i
. Maybe, that is the problem?
Also, the way you've described the problem, I think the correct solution is this:
for (int i = 0; i < this->d_packetlen - 2; i++)
{
payload << static_cast<unsigned int>(this->d_packet[i]);
}
for (int j = this->d_packetlen - 2; j < this->d_packetlen; j++)
{
crc << static_cast<unsigned int>(this->d_packet[j]);
}
That is the second loop should be outside the first loop.
I think the problem is that your nested loop increments i
instead of j
.
for (int i = 0; i < this->d_packetlen - 2; i++)
{
payload << static_cast<unsigned>(this->d_packet[i]);
for (int j = i; j < this->d_packetlen; i++ /* <=== HERE */)
{
crc << static_cast<unsigned>(this->d_packet[j]);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.