Placing bytes into a stream

J

Jonathan Halterman

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. 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
 
M

Mike Wahler

Jonathan Halterman said:
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
 
J

Jonathan Halterman

Thanks for the example Mike. I will give this "deserialization" method a
try.

jonathan

Mike Wahler said:
Jonathan Halterman said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top