[英]How can I format values from std::vector to std::string efficiently?
I have a program that takes a std::vector<uint8_t>
and returns a std::string
formatted as hex characters followed by the ASCII text like so: 我有一个程序,使用std::vector<uint8_t>
并返回一个std::string
格式为十六进制字符,后跟ASCII文本,如下所示:
03 00 00 54 00 00 00 08 00 00 00 00 00 00 00 00 ASCII ...T............ 03 00 00 54 00 00 00 08 00 00 00 00 00 00 00 00 ASCII ... T ............
74 21 B8 30 00 2C 2E 31 62 30 74 21 A8 30 00 2C ASCII t!.0.,.1b0t!.0., 74 21 B8 30 00 2C 2E 31 62 30 74 21 A8 30 00 2C ASCII t!.0。,。1b0t!.0。,
This is the main part of the code: 这是代码的主要部分:
std::vector<uint8_t> value;
std::stringstream printed_line;
std::stringstream tempHexLine;
std::stringstream tempAsciiLine;
for (size_t printed_bytes = 0; printed_bytes < value.size(); printed_bytes += bytes_line) {
for (int i = 0; i < bytes_line; ++i) {
tempHexLine << std::setfill('0') << std::setw(2) << std::uppercase << std::hex << static_cast<uint16_t>(value[printed_bytes + i]) << " ";
}
for (int i = 0; i < bytes_line; ++i) {
if(isprint(value[printed_bytes + i])) {
if (value[printed_bytes + i] == 60) {
tempAsciiLine << "<";
} else if(value[printed_bytes + i] == 62) {
tempAsciiLine << ">";
} else {
tempAsciiLine << static_cast<char>(value[printed_bytes + i]);
}
} else {
tempAsciiLine << ".";
}
}
}
printed_line << tempHexLine.str() << " " << tempAsciiLine .str();
The problem that I'm trying to solve is that when the vector size is large (> 1000 elements), the this takes a long time - approximately 70% of the samples using Very Sleepy where to the ::operator<<
function. 我要解决的问题是,当向量大小较大(> 1000个元素)时,这将花费很长时间-大约70%的样本使用::operator<<
函数的Very Sleepy位置。
What is the fastest way to apply a format like this to the values in the vector? 将这样的格式应用于向量中值的最快方法是什么? There are well defined chucks of data that the vector needs to be broken into, so it seems like outputting a byte at a time is an inefficient way. 向量需要细分为明确定义的数据块,因此一次输出一个字节似乎是一种低效的方式。
This should be trivial to write efficiently. 要有效地编写,这应该是微不足道的。 Try this: 尝试这个:
#include <algorithm>
#include <cstdint>
#include <iterator>
#include <string>
#include <vector>
std::string to_hex(std::vector<uint8_t> const & v)
{
std::string result;
result.reserve(4 * v.size() + 6);
for (uint8_t c : v)
{
static constexpr char alphabet[] = "0123456789ABCDEF";
result.push_back(alphabet[c / 16]);
result.push_back(alphabet[c % 16]);
result.push_back(' ');
}
result.append("ASCII ", 6);
std::copy(v.begin(), v.end(), std::back_inserter(result));
return result;
}
You might use some conversion tables for individual characters: 您可以将一些转换表用于单个字符:
#include <cctype>
#include <cstring>
#include <iostream>
#include <vector>
class HexadecimalTable
{
public:
static const unsigned ElementSize = 4;
HexadecimalTable();
const char* operator [] (std::uint8_t value) const {
return m_table[value];
}
private:
typedef char hex_type[ElementSize];
hex_type m_table[256];
};
HexadecimalTable::HexadecimalTable() {
static const char hex[] = "0123456789ABCDEF";
for(int i = 0; i < 256; ++i) {
m_table[i][0] = hex[i / 16];
m_table[i][1] = hex[i % 16];
m_table[i][2] = 0;
}
}
class EntityTable
{
public:
static const unsigned ElementSize = 8;
EntityTable();
const char* operator [] (std::uint8_t value) const {
return m_table[value];
}
private:
typedef char entity_type[ElementSize];
entity_type m_table[256];
};
EntityTable::EntityTable() {
// Printable and non printable characters
for(int i = 0; i < 256; ++i) {
if(std::isprint(i)) m_table[i][0] = char(i);
else m_table[i][0] = '.';
m_table[i][1] = 0;
}
// Special characters
std::strcpy(m_table['<'], "<");
std::strcpy(m_table['>'], ">");
// ...
}
std::string hex_display(std::vector<uint8_t> const & v) {
HexadecimalTable hexadecimal;
EntityTable entities;
std::string result;
result.reserve((HexadecimalTable::ElementSize + EntityTable::ElementSize) * v.size());
for (uint8_t c : v) {
result.append(hexadecimal[c]);
result.push_back(' ');
}
for (uint8_t c : v)
result.append(entities[c]);
return result;
}
int main()
{
std::vector<std::uint8_t> v = {
0x74, 0x21, 0xB8, 0x30,
0x00, 0x2C, 0x2E, 0x31,
0x62, 0x30, 0x74, 0x21,
0xA8, 0x30, 60, 62
};
std::cout << hex_display(v) << std::endl;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.