471,111 Members | 1,514 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,111 software developers and data experts.

streams implementation - byte buffer

A few days ago I recieved yet again advice on implementing a buffer of
bytes. At issue: (part 1) how do I take the contents of a struct,
then dump (used sparingly) it into a byte buffer. Similarily, take the
contents of a (part 2) buffer and dump (used sparingly) it into a

So I found an implementation that handles part 1. Well partially.

#include <ostream>
namespace jngcomp { namespace utils
// Output string buffer class
class stringbuf : public std::streambuf
stringbuf(char* buffer, int buflen)
: m_buffer(buffer), m_buflen(buflen)
setp(m_buffer, m_buffer + m_buflen);
char* getpptr()
return pptr();
void setpptr(char* p)
setp(p, m_buffer + m_buflen);
virtual int_type overflow(int_type c)
return EOF;
char* m_buffer;
int m_buflen;

// Output string stream class which is suppled with a buffer to
write to.
// Ie, stream equivalent of sprintf.
class ostringstream : public std::ostream
ostringstream(char* buffer, int buflen)
: m_buf(buffer, buflen), std::ostream(&m_buf)
char* getpptr()
return m_buf.getpptr();
void setpptr(char* p)
stringbuf m_buf;

struct test {
unsigned int idx : 16;
unsigned int jdx : 32;
bool kdx : 1;

int main()
test t_;
t_.idx = 15;
t_.jdx = 25;

char buffer[10];
jngcomp::utils::ostringstream strm(buffer, sizeof(buffer));
strm << t_.idx ;
std::cout << buffer[0] << std::endl;
std::cout << buffer[1] << std::endl;

What's interesting to me is the output is

With this approach how does one get 15 to spread across 4 bytes
withouth having to do
strm << 0 << 0 << t_.idx; ?

Tips/hints on part 2?

Jul 23 '05 #1
4 6543
seems like what you posted has some BIG mistakes...
for simple structs as yours I think it's enough to copy memory byte by

test the_struct;
unsigned char buf[128]; //assume it
//has enough space to hold the_struct

//to buffer:

//from buffer:
this should answer part1, part2... but the need for this itself is very

Jul 23 '05 #2
Unless I'm missing something your solution assumes that padding etc
with regard to compiler a and b on two different platforms are the
same. Wont work well for tranmitting data between different platfoms.
The struct shown was for 'demonstration' purposes. The 'real' struct
is quite large.

Jul 23 '05 #3
> dump (used sparingly) it into a byte buffer
means to copy to a byte buffer and nothing else.

I didn't look inside that jngcomp::utils::ostringstream and probably
all it does is the same as printing to a stream, e.g. it converts 15
into "15" and nothing else, that's why you get 1 and 5. From the first
glance I thought there was something wrong with the code because you
put in integers into buffer and access then as bytes.

If you are interested how to make it work yourself, you may provide
friend operators for >> and << and print/read into your strutcs to/from
a stream. Example:
struct test {
unsigned int idx : 16;
unsigned int jdx : 32;
bool kdx : 1;
friend ostream& operator<<(ostream& s, const test& t){
return s<<t.idx<<'\n'<<t.jdx<<'\n'<<t.kde<<'\n';
... similar for operator>>

but I'd better use something that's already developed and tested by
others, for example boost.serialization

Jul 23 '05 #4
>>> serialization

Somehow I overlooked the boost library (once again). Adimittidely,
this is pretty swanky and quite sophisticated for my level but I'm
almost convinced the solution lies here in serialization.

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by bartek | last post: by
8 posts views Thread by kelvSYC | last post: by
30 posts views Thread by Chris McDonald | last post: by
2 posts views Thread by Peter Rilling | last post: by
3 posts views Thread by Kirit Sælensminde | last post: by
6 posts views Thread by Ioannis Papadopoulos | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.