简体   繁体   English

如何有效地将值从std :: vector格式化为std :: string?

[英]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 << "&lt;";

                } else if(value[printed_bytes + i] == 62) {
                    tempAsciiLine << "&gt;";

                } 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['<'], "&lt;");
    std::strcpy(m_table['>'], "&gt;");
    // ...
}

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.

相关问题 如果我使用 std:find 在向量对中找到一个元素,如何将向量中的值转换为字符串? - If I find an alement in a vector pairs using std:find, how can I get the values from the vector into a string? 如何有效地将std :: string复制到向量中 - How to efficiently copy a std::string into a vector 如何构建一个std :: vector <std :: string>然后对它们进行排序? - How can I build a std::vector<std::string> and then sort them? 如何将 std::string 转换为 std::vector? - How can I convert a std::string to std::vector? 如何有效地将std :: vector <char>复制到std :: string - How to efficiently copy a std::vector<char> to a std::string 如何从std :: map值的std :: vector找到最小键值? - How can I find the minimum key value from an std::vector of std::map values? 如何用来自另一个 std::string 的迭代值有效地替换 std::string 中的字符? - How to efficiently replace characters in an std::string with iterative values from another std::string? 如何按不同 std::vector 的值对 std::vector 进行排序? - How do I sort a std::vector by the values of a different std::vector? 在std :: vector中存储std :: string的十六进制值,反之亦然? - Storing hexadecimal values from a std::string in a std::vector and vise versa? 如何将具有十六进制值的 std::string 转换为 std::vector<unsigned char></unsigned> - How to convert std::string with hex values into std::vector<unsigned char>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM