Placing bytes into a stream



 DEVELOP > c-Plus-Plus > Placing bytes into a stream

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Jonathan Halterman"
Date: 15 Oct 2003 08:00:48 PM
Object: Placing bytes into a stream
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
.

User: "Mike Wahler"

Title: Re: Placing bytes into a stream 15 Oct 2003 08:39:31 PM
"Jonathan Halterman" <nix@aol.com> wrote in message
news:vorrftkh99u9bc@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
.
User: "Mike Wahler"

Title: Re: Placing bytes into a stream 16 Oct 2003 04:19:27 AM
"Mike Wahler" <mkwahler@mkwahler.net> wrote in message
news:nFmjb.2557$s93.1280@newsread3.news.pas.earthlink.net...


void foo(const char* buffer)
{
const long *pl(reinterpret_cast<long*>(buffer));

const long *pl = reinterpret_cast<long*>(buffer);

long L1 = *pl++;
long L2 = *pl++;

const int *pi(reinterpret_cast<int*>(pl);

const int *pi = reinterpret_cast<int*>(pl);

Not tested.

Or compiled. :-)
-Mike
.

User: "Jonathan Halterman"

Title: Re: Placing bytes into a stream 15 Oct 2003 08:56:36 PM
Thanks for the example Mike. I will give this "deserialization" method a
try.
jonathan
"Mike Wahler" <mkwahler@mkwahler.net> wrote in message
news:nFmjb.2557$s93.1280@newsread3.news.pas.earthlink.net...


"Jonathan Halterman" <nix@aol.com> wrote in message
news:vorrftkh99u9bc@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


.
User: "Jonathan Mcdougall"

Title: Re: Placing bytes into a stream 15 Oct 2003 11:45:58 PM

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.

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

Please do not top-post.
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4
Jonathan
.
User: "Jonathan Halterman"

Title: Re: Placing bytes into a stream 16 Oct 2003 04:53:37 PM
"Jonathan Mcdougall" <jonathanmcdougall@DELyahoo.ca> wrote in message
news:CJpjb.6050$rj4.448988@weber.videotron.net...

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.

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



Please do not top-post.

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4

Bet you were waiting all week to say that to someone. Well done.
jonathan
.





  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER