Come convertire un array di caratteri in corda a base esagonale-stream (ostringstream)
-
25-09-2019 - |
Domanda
in C ++ (su Linux con gcc) Mi piacerebbe mettere un array di byte (vector<unsigned char>
) per un ostringstream
o un string
.
So che posso usare sprintf
ma non sembra essere il modo migliore per utilizzare char*
anche.
A proposito: questo Link non ha aiutato
Modifica Tutti rispondono lavoro svolto finora. Ma non ho meantion, che mi piacerebbe convertire i / esadecimale valori byte nella loro rappresentazione di stringa, per esempio, vector<..> = {0,1,2} -> string = "000102"
. Ci scusiamo per questo dettaglio mancante, ma importante
Soluzione
poche possibilità di ottenere un up-voto, ma dal momento che è esattamente quello che il PO ha chiesto, e nessun altra risposta, tra cui quello selezionato, stranamente, lo fa:
#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();
}
alternativo flusso Dump
Se tutto quello che vogliamo fare è scaricare un char matrice fissa unsigned Di seguito vi comodamente fare così, che quasi nessuno in testa a tutti.
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;
}
Io uso questo tutto il tempo quando voglio scaricare i buffer fissi a un derivitive ostream. La chiamata è morto semplice:
unsigned char data[64];
...fill data[] with, well.. data...
cout << data << endl;
Altri suggerimenti
Da vettore char arr a stringa STL:
std::string str(v.begin(), v.end());
Da stringa stl a matrice vettore char:
std::string str = "Hellow World!";
std::vector<unsigned char> v(str.begin(), str.end());
Per setw includono:
#include <iomanip>
Questo dovrebbe mettere 01 nel flusso:
std::ostringstream oss;
unsigned char byte = 0x01;
oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte);
Usa boost :: :: alogorithm hex
std::vector<unsigned char> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
Per std :: ostream
std::ostream ss;
boost::algorithm::hex(v.begin(), v.end(), std::ostream_iterator<char>(ss));
Per std :: string
std::string res;
boost::algorithm::hex(v.begin(), v.end(), back_inserter(res));
Così si desidera inserire i dati di un vettore di caratteri in una stringa? Facile:
string str;
str.resize(vec.size());
for (int n=0;n<vec.size();n++) {
str[n] = vec[n];
}