Created
November 22, 2023 15:36
-
-
Save Jackarain/367b1572a1131079d8eb6d6dd50d9acf to your computer and use it in GitHub Desktop.
print_data_len
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void print_data_len(const std::vector<uint8_t>& data, uint16_t target_len = 0x7FFF) | |
{ | |
if (data.size() < 4) { | |
std::cerr << "数据长度小于4,无法转换为 uint16_t。" << std::endl; | |
return; | |
} | |
std::cout << "print len: " << data.size() << "\n"; | |
uint16_t p1 = 0; | |
p1 = data[0] | (data[1] << 8); | |
bool found = false; | |
int count = 0; | |
// 从第二个字节开始,确保每个 uint16_t 的前一个字节是前一个 uint16_t 的后一个字节 | |
for (size_t i = 1; i < data.size(); i += 2) { | |
// 组合当前字节和前一个字节为 uint16_t | |
uint16_t value = static_cast<uint16_t>(data[i]) | (static_cast<uint16_t>(data[i - 1]) << 8); | |
// 设置字段宽度和左对齐 | |
std::cout << std::left << std::setw(10) << value; | |
if ((p1 & value) < target_len && !found && i != 1) { | |
found = true; | |
std::cout << "decode len: " << (p1 & value) << "\n"; | |
} | |
p1 = value; | |
count++; | |
if (count == 8) { // 每输出8个数值后换行 | |
std::cout << std::endl; | |
count = 0; | |
} | |
} | |
if (count > 0) { | |
std::cout << std::endl; // 保证最后一行在输出后有换行 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment