Placing bytes into a stream

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonathan Halterman

    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


  • Mike Wahler

    #2
    Re: Placing bytes into a stream


    "Jonathan Halterman" <nix@aol.com> wrote in message
    news:vorrftkh99 u9bc@corp.super news.com...[color=blue]
    > I have a pointer to a buffer of bytes that I receive from a tcp stream[/color]
    that[color=blue]
    > I would like to place into some kind of stream that can I can then use to
    > serialize the data into into individual variables.[/color]

    This is *de*serializati on.
    [color=blue]
    >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[/color]
    that[color=blue]
    > there are two long values (4 bytes each), followed by two integers, then a
    > 20 byte string of characters, etc...
    >
    > jonathan
    >
    >[/color]

    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*>(bu ffer));
    long L1 = *pl++;
    long L2 = *pl++;

    const int *pi(reinterpret _cast<int*>(pl) ;
    int I1 = *pi++;
    int I2 = *pi++;

    const char *p = reinterpret_cas t<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


    Comment

    • Jonathan Halterman

      #3
      Re: Placing bytes into a stream

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

      jonathan

      "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
      news:nFmjb.2557 $s93.1280@newsr ead3.news.pas.e arthlink.net...[color=blue]
      >
      > "Jonathan Halterman" <nix@aol.com> wrote in message
      > news:vorrftkh99 u9bc@corp.super news.com...[color=green]
      > > I have a pointer to a buffer of bytes that I receive from a tcp stream[/color]
      > that[color=green]
      > > I would like to place into some kind of stream that can I can then use[/color][/color]
      to[color=blue][color=green]
      > > serialize the data into into individual variables.[/color]
      >
      > This is *de*serializati on.
      >[color=green]
      > >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[/color][/color]
      am[color=blue][color=green]
      > > 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[/color]
      > that[color=green]
      > > there are two long values (4 bytes each), followed by two integers, then[/color][/color]
      a[color=blue][color=green]
      > > 20 byte string of characters, etc...
      > >
      > > jonathan
      > >
      > >[/color]
      >
      > 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*>(bu ffer));
      > long L1 = *pl++;
      > long L2 = *pl++;
      >
      > const int *pi(reinterpret _cast<int*>(pl) ;
      > int I1 = *pi++;
      > int I2 = *pi++;
      >
      > const char *p = reinterpret_cas t<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
      >
      >[/color]


      Comment

      • Jonathan Mcdougall

        #4
        Re: Placing bytes into a stream

        >>> I have a pointer to a buffer of bytes that I receive from a tcp[color=blue][color=green][color=darkred]
        >>> 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.[/color]
        >>
        >> This is *de*serializati on.
        >>[/color]
        > Thanks for the example Mike. I will give this "deserializatio n"
        > method a try.[/color]


        Please do not top-post.




        Jonathan


        Comment

        • Mike Wahler

          #5
          Re: Placing bytes into a stream

          "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
          news:nFmjb.2557 $s93.1280@newsr ead3.news.pas.e arthlink.net...[color=blue]
          >
          > void foo(const char* buffer)
          > {
          > const long *pl(reinterpret _cast<long*>(bu ffer));[/color]

          const long *pl = reinterpret_cas t<long*>(buffer );
          [color=blue]
          > long L1 = *pl++;
          > long L2 = *pl++;
          >
          > const int *pi(reinterpret _cast<int*>(pl) ;[/color]

          const int *pi = reinterpret_cas t<int*>(pl);
          [color=blue]
          > Not tested.[/color]

          Or compiled. :-)

          -Mike



          Comment

          • Jonathan Halterman

            #6
            Re: Placing bytes into a stream


            "Jonathan Mcdougall" <jonathanmcdoug all@DELyahoo.ca > wrote in message
            news:CJpjb.6050 $rj4.448988@web er.videotron.ne t...[color=blue][color=green][color=darkred]
            > >>> 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*serializati on.
            > >>[/color]
            > > Thanks for the example Mike. I will give this "deserializatio n"
            > > method a try.[/color]
            >
            >
            > Please do not top-post.
            >
            > http://www.parashift.com/c++-faq-lit...t.html#faq-5.4[/color]

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

            jonathan


            Comment

            Working...