Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a vector<char> to a string with a conversion

Tags:

c++

I'd like to convert a vector<char> to a std::string and do a conversion one the way.

I'm almost there, but the result of the code below is a vector<string>, while I'd like to have one string (a concatenation of all the string parts in the vector).

See my code example for details.

string ConvertHexToAscii(const char input)
{
    std::ostringstream oss;
    oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(input);
    return oss.str();
}

vector<char> readBuffer; // this is my input

readBuffer.push_back(0x1c);
readBuffer.push_back(0x09);

vector<string> resultVec;

std::transform(readBuffer.begin(), readBuffer.end()
    , back_inserter(resultVec)
    , ConvertHexToAscii);

// resultVec[0] = "1C";
// resultVec[1] = "09";

The result I need is a string containing "1C09". How to achieve that with std::transform?

like image 202
nabulke Avatar asked Jun 28 '12 09:06

nabulke


People also ask

How do I copy a vector to a string?

Using std::accumulate Another option to convert a vector to a string is using the standard function std::accumulate , defined in the header <numeric> . We can overwrite its default operation by providing a binary predicate to perform the concat operation on two strings and return the result.

How do you return a vector string in C++?

Returning a Vector Pointervector<string> *v = fn(&store); respectively. Note the presence and position of * in the return type of the function definition.


3 Answers

You were almost there; this works:

std::stringstream sstr;
std::transform(
    input.begin(), input.end(),
    std::ostream_iterator<std::string>(sstr, ""),
    ConvertHexToAscii);

But unfortunately this instantiates quite a lot of string streams, which is inefficient. Ideally, the ConvertHexToAscii (misnamed, by the way! C++ doesn’t know about encodings) function would directly use the underlying stream.

like image 59
Konrad Rudolph Avatar answered Sep 20 '22 04:09

Konrad Rudolph


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

std::string ConvertHexToAscii(std::string acc, char input)
{
  std::ostringstream oss;
  oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(input);
  return acc + oss.str();
}


int main() {
  std::vector<char> readBuffer; // this is my input
  readBuffer.push_back(0x1c);
  readBuffer.push_back(0x09);

  std::cout << std::accumulate(readBuffer.begin(), readBuffer.end()
          , std::string(), ConvertHexToAscii) << std::endl;

  return 0;
}
like image 33
perreal Avatar answered Sep 24 '22 04:09

perreal


create your own back_insert_iterator (look at the code in your stl lib, it's fairly simple) for string types of which operator = is defined as

template< class string_type, class value_type >
class back_insert_iterator
{
public:
  back_insert_iterator< _string_type >& operator = ( const value_type& val )
  {
    container->append( val )
    return *this;
  }
};
like image 40
stijn Avatar answered Sep 22 '22 04:09

stijn