代码之家  ›  专栏  ›  技术社区  ›  tuergeist

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

  •  4
  • tuergeist  · 技术社区  · 15 年前

    在C++(在Linux上使用GCC),我想放一个字节数组( vector<unsigned char> )到 ostringstream 或者 string .

    我知道我可以用 sprintf char* 也。

    顺便说一句: this link did not help

    到目前为止,所有答案都有效。但我不是说,我想把字节/十六进制值转换成它们的字符串表示形式,例如。, vector<..> = {0,1,2} -> string = "000102" . 抱歉,遗漏了重要的细节

    5 回复  |  直到 9 年前
        1
  •  9
  •   WhozCraig    13 年前

    几乎没有机会获得投票,但既然是这样 确切地

    #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();
    }
    

    交替流转储

    如果您只想转储一个无符号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;
    }
    

    unsigned char data[64];
    ...fill data[] with, well.. data...
    cout << data << endl;
    
        2
  •  8
  •   wengseng    15 年前

    从vector char arr到stl string:

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

    从stl字符串到矢量字符数组:

    std::string str = "Hellow World!";
    std::vector<unsigned char> v(str.begin(), str.end());
    
        3
  •  3
  •   xoride    15 年前

    对于setw,包括:

    #include <iomanip>
    

    std::ostringstream oss;
    
    unsigned char byte = 0x01;
    oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte);
    
        4
  •  3
  •   Like    11 年前

    使用 boost::alogorithm::hex

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

    标准::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));
    
        5
  •  1
  •   Alexander Rafferty    15 年前

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