Placing bytes into a stream

Discussion in 'C++' started by Jonathan Halterman, Oct 16, 2003.

  1. 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
    Jonathan Halterman, Oct 16, 2003
    #1
    1. Advertising

  2. Jonathan Halterman

    Mike Wahler Guest

    "Jonathan Halterman" <> wrote in message
    news:...
    > 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
    Mike Wahler, Oct 16, 2003
    #2
    1. Advertising

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

    jonathan

    "Mike Wahler" <> wrote in message
    news:nFmjb.2557$...
    >
    > "Jonathan Halterman" <> wrote in message
    > news:...
    > > 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
    >
    >
    Jonathan Halterman, Oct 16, 2003
    #3
  4. >>> 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
    Jonathan Mcdougall, Oct 16, 2003
    #4
  5. Jonathan Halterman

    Mike Wahler Guest

    "Mike Wahler" <> wrote in message
    news:nFmjb.2557$...
    >
    > 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
    Mike Wahler, Oct 16, 2003
    #5
  6. "Jonathan Mcdougall" <> wrote in message
    news:CJpjb.6050$...
    > >>> 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
    Jonathan Halterman, Oct 16, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Collins
    Replies:
    3
    Views:
    5,997
    Jason Collins
    Feb 18, 2004
  2. mrby

    4-bytes or 8-bytes alignment?

    mrby, Nov 2, 2004, in forum: C Programming
    Replies:
    8
    Views:
    405
    Mark McIntyre
    Nov 2, 2004
  3. Replies:
    5
    Views:
    520
    Flash Gordon
    Apr 9, 2006
  4. Yandos
    Replies:
    12
    Views:
    5,101
    Pete Becker
    Sep 15, 2005
  5. Replies:
    8
    Views:
    486
    Bob Hairgrove
    Apr 10, 2006
Loading...

Share This Page