如何将字符数组转换为基于字符串的十六进制流(ostringstream)

发布于 2024-09-24 21:25:15 字数 494 浏览 0 评论 0原文

在 C++ 中(在使用 gcc 的 Linux 上)我想将字节数组(vector)放入 ostringstreamstring

我知道我可以使用 sprintf,但它似乎也不是使用 char* 的最佳方法。

顺便说一句: 此链接没有帮助

编辑: 到目前为止,所有答案都有效。但我并不是说我想将字节/十六进制值转换为其字符串表示形式,例如 vector<..> = {0,1,2} ->字符串 =“000102”。抱歉缺少但重要的细节

in C++ (on Linux with gcc) I'd like to put a byte array (vector<unsigned char>) to a ostringstream or a string.

I know that I can use sprintf but it doesn't seem to be the best way to use char* also.

btw: this link did not help

Edit: All answer work so far. But I did not meantion, that I'd like to convert the bytes/hex-values into their string representation, e.g., vector<..> = {0,1,2} -> string = "000102". Sorry for that missing but important detail

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

雨后彩虹 2024-10-01 21:25:15

获得赞成票的机会很小,但由于这正是OP所要求的,并且没有其他答案,包括所选的答案,奇怪的是,这样做:

#include <iostream>
#include <sstream>
#include <vector>
#include <iomanip>

// used by bin2hex for conversion via stream.
struct bin2hex_str
{
    std::ostream& os;
    bin2hex_str(std::ostream& os) : os(os) {}
    void operator ()(unsigned char ch)
    {
        os << std::hex
        << std::setw(2)
        << static_cast<int>(ch);
    }
};

// convert a vector of unsigned char to a hex string
std::string bin2hex(const std::vector<unsigned char>& bin)
{
    std::ostringstream oss;
    oss << std::setfill('0');
    std::for_each(bin.begin(), bin.end(), bin2hex_str(oss));
    return oss.str();
}

// or for those who wish for a C++11-compliant version
std::string bin2hex11(const std::vector<unsigned char>& bin)
{
    std::ostringstream oss;
    oss << std::setfill('0');
    std::for_each(bin.begin(), bin.end(),
        [&oss](unsigned char ch)
        {
            oss << std::hex
            << std::setw(2)
            << static_cast<int>(ch);
        });
    return oss.str();
}

备用流转储 strong>

如果您只想转储一个 unsigned char 固定数组,则以下操作将轻松执行此操作,几乎没有任何开销。

template<size_t N>
std::ostream& operator <<(std::ostream& os, const unsigned char (&ar)[N])
{
    static const char alpha[] = "0123456789ABCDEF";
    for (size_t i=0;i<N;++i)
    {
        os.put(alpha[ (ar[i]>>4) & 0xF ]);
        os.put(alpha[ ar[i] & 0xF ]);
    }
    return os;
}

当我想将固定缓冲区转储到 ostream 衍生物时,我总是使用它。这个调用非常简单:

unsigned char data[64];
...fill data[] with, well.. data...
cout << data << endl;

Little chance of getting an up-vote, but since it is exactly what the OP asked for, and no other answer, including the selected one, oddly, does so:

#include <iostream>
#include <sstream>
#include <vector>
#include <iomanip>

// used by bin2hex for conversion via stream.
struct bin2hex_str
{
    std::ostream& os;
    bin2hex_str(std::ostream& os) : os(os) {}
    void operator ()(unsigned char ch)
    {
        os << std::hex
        << std::setw(2)
        << static_cast<int>(ch);
    }
};

// convert a vector of unsigned char to a hex string
std::string bin2hex(const std::vector<unsigned char>& bin)
{
    std::ostringstream oss;
    oss << std::setfill('0');
    std::for_each(bin.begin(), bin.end(), bin2hex_str(oss));
    return oss.str();
}

// or for those who wish for a C++11-compliant version
std::string bin2hex11(const std::vector<unsigned char>& bin)
{
    std::ostringstream oss;
    oss << std::setfill('0');
    std::for_each(bin.begin(), bin.end(),
        [&oss](unsigned char ch)
        {
            oss << std::hex
            << std::setw(2)
            << static_cast<int>(ch);
        });
    return oss.str();
}

Alternate Stream Dump

If all you want to do is dump an unsigned char fixed array the following will handily do so, which almost no overhead at all.

template<size_t N>
std::ostream& operator <<(std::ostream& os, const unsigned char (&ar)[N])
{
    static const char alpha[] = "0123456789ABCDEF";
    for (size_t i=0;i<N;++i)
    {
        os.put(alpha[ (ar[i]>>4) & 0xF ]);
        os.put(alpha[ ar[i] & 0xF ]);
    }
    return os;
}

I use this all the time when I want to dump fixed buffers to an ostream derivitive. The call is dead-simple:

unsigned char data[64];
...fill data[] with, well.. data...
cout << data << endl;
爱冒险 2024-10-01 21:25:15

从向量 char arr 到 stl 字符串:

std::string str(v.begin(), v.end());

从 stl 字符串到向量 char 数组:

std::string str = "Hellow World!";
std::vector<unsigned char> v(str.begin(), str.end());

From vector char arr to stl string:

std::string str(v.begin(), v.end());

From stl string to vector char array:

std::string str = "Hellow World!";
std::vector<unsigned char> v(str.begin(), str.end());
孤者何惧 2024-10-01 21:25:15

对于 setw include:

#include <iomanip>

这应该将 01 放入流中:

std::ostringstream oss;

unsigned char byte = 0x01;
oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte);

For setw include:

#include <iomanip>

This should put 01 in stream:

std::ostringstream oss;

unsigned char byte = 0x01;
oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte);
黯然 2024-10-01 21:25:15

使用 boost:: alogorithm::hex

std::vector<unsigned char> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);

对于 std::ostream

std::ostream ss;
boost::algorithm::hex(v.begin(), v.end(), std::ostream_iterator<char>(ss));

对于 std::string

std::string res;
boost::algorithm::hex(v.begin(), v.end(), back_inserter(res));

Use boost::alogorithm::hex

std::vector<unsigned char> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);

For std::ostream

std::ostream ss;
boost::algorithm::hex(v.begin(), v.end(), std::ostream_iterator<char>(ss));

For std::string

std::string res;
boost::algorithm::hex(v.begin(), v.end(), back_inserter(res));
吻安 2024-10-01 21:25:15

那么您想将字符向量的数据放入字符串中吗?简单的:

string str;
str.resize(vec.size());
for (int n=0;n<vec.size();n++) {
  str[n] = vec[n];
}

So you want to put the data of a vector of chars into a string? Easy:

string str;
str.resize(vec.size());
for (int n=0;n<vec.size();n++) {
  str[n] = vec[n];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文