简体   繁体   English

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

[英]How to convert a char array into string based hex-stream (ostringstream)

in C++ (on Linux with gcc) I'd like to put a byte array ( vector<unsigned char> ) to a ostringstream or a string . 在C ++中(在Linux上使用gcc)我想把一个字节数组( vector<unsigned char> )放到ostringstreamstring

I know that I can use sprintf but it doesn't seem to be the best way to use char* also. 我知道我可以使用sprintf但它似乎也不是使用char*的最佳方式。

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, eg, vector<..> = {0,1,2} -> string = "000102" . 但我没有意思,我想将字节/十六进制值转换为它们的字符串表示,例如, vector<..> = {0,1,2} -> string = "000102" Sorry for that missing but important detail 对不起,但重要的细节

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: 获得投票的可能性很小,但因为这正是 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();
}

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. 如果你想要做的是转储为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;
}

I use this all the time when I want to dump fixed buffers to an ostream derivitive. 当我想将固定缓冲区转储到ostream派生时,我一直使用它。 The call is dead-simple: 这个电话很简单:

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

From vector char arr to stl string: 从vector char arr到stl string:

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

From stl string to vector char array: 从stl字符串到vector char数组:

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

Use boost::alogorithm::hex 使用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

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

For std::string 对于std :: string

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

For setw include: 对于setw包括:

#include <iomanip>

This should put 01 in stream: 这应该把01放在流中:

std::ostringstream oss;

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

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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM