D
David Rubin
How can I stream data to a raw char buffer without using strstream
(which is deprecated), sprintf(), or any copy operation? Here is my
function:
const char *i2bs(unsigned int i, char *buf, int length)
{
// Generate a bit string representation of 'i'. Store the result
in
// 'buf' and return 'buf'. The behavior is undefined if 'buf' is
// null, or 'length' is not large enough to store the result.
static const char *tab[] = // byte to bit conversion table
{
"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111",
};
/*
std::streambuf sbuf; // cannot create basic_streambuf!
sbuf.pubsetbuf(buf, length);
std:stream os(sbuf);
os << tab[(i >>28) & 0xf] << tab[(i >>24) & 0xf]
<< tab[(i >>20) & 0xf] << tab[(i >>16) & 0xf]
<< tab[(i >>12) & 0xf] << tab[(i >> 8) & 0xf]
<< tab[(i >> 4) & 0xf] << tab[(i >> 0) & 0xf];
*/
return buf;
}
/david
(which is deprecated), sprintf(), or any copy operation? Here is my
function:
const char *i2bs(unsigned int i, char *buf, int length)
{
// Generate a bit string representation of 'i'. Store the result
in
// 'buf' and return 'buf'. The behavior is undefined if 'buf' is
// null, or 'length' is not large enough to store the result.
static const char *tab[] = // byte to bit conversion table
{
"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111",
};
/*
std::streambuf sbuf; // cannot create basic_streambuf!
sbuf.pubsetbuf(buf, length);
std:stream os(sbuf);
os << tab[(i >>28) & 0xf] << tab[(i >>24) & 0xf]
<< tab[(i >>20) & 0xf] << tab[(i >>16) & 0xf]
<< tab[(i >>12) & 0xf] << tab[(i >> 8) & 0xf]
<< tab[(i >> 4) & 0xf] << tab[(i >> 0) & 0xf];
*/
return buf;
}
/david