"Jonathan Halterman" <ni*@aol.com> wrote in message
news:vo************@corp.supernews.com...
I have a pointer to a buffer of bytes that I receive from a tcp stream
that I would like to place into some kind of stream that can I can then use to
serialize the data into into individual variables.
This is *de*serialization.
I looked at the
streambuf, istream, and strstream, and can't quite figure out a simple
implementation for what I need.
Given a pointer to a buffer of bytes, and the size of the bytes that I am
concerned with, how can I get this into a stream that I can then use to
serialize the data to individual variables? The data is organized such
that there are two long values (4 bytes each), followed by two integers, then a
20 byte string of characters, etc...
jonathan
By definition, the size of a char is one byte in C++.
If the number of bits per char in the buffer don't
match your implementation's type 'char' number of bits,
you'll need to do a conversion.
I'm assuming your description above is indicating that
your implementation's type 'long' is actually 4 bytes
in size, and that the 'int' types in the buffer also
match the size for your implementation.
You may also need to 'fix up' the byte ordering
(e.g. 'big-endian', 'little-endian')
#include <algorithm>
#include <iostream>
void foo(const char* buffer)
{
const long *pl(reinterpret_cast<long*>(buffer));
long L1 = *pl++;
long L2 = *pl++;
const int *pi(reinterpret_cast<int*>(pl);
int I1 = *pi++;
int I2 = *pi++;
const char *p = reinterpret_cast<char *>(pi);
char array[21] = {0}
std:: copy(p, p + 20, array);
std::cout << L1 << '\n'
<< L2 << '\n'
<< I1 << '\n'
<< I2 << '\n'
<< array << '\n';
}
Not tested.
-Mike