Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. serialize using stream. - binary

serialize using stream. - binary

Scheduled Pinned Locked Moved ATL / WTL / STL
questionc++sysadminperformancetutorial
3 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    magicast
    wrote on last edited by
    #1

    hi~ I apologize being not good at English. I wanna serialize my own data for network/file/etc... I thought that stream of c++ standard library is suitable. but, there's no facilities exactly for my use. 1. I wanna store stream data into memory.(not file) 2. insertion and extraction must operate in binary mode. // example of usage // proc A omystream ms; // mystream is that I wanted.. int n = 1; short s = 2; ms << n << s; // or ms << int_b(n) << short_b(s); // int_b(), short_b() is custom manipulators ::send(ms.buf(),...); // send to network // proc B BYTE buf[4096]; ::revc(buf,...); // receive from network imystream ms(buf, dwSizeReceived); // dwSizeReceived = 6 int n; short s; ms >> n >> s; // n = 1, s = 2 // or ms >> int_b(n) >> short_b(s); // result data occupy 6 bytes memory. (4 for n(int), 2 for s(short)) send and receive 6 byte of memory. memory layout: 00 00 00 01 00 02 // 6 byte not "000000010002" // 12 byte this mean not string but binary. how can I achieve it? derive strstream or stream? derive stream_buf? I need your advice. :) thanks in advance.

    A B 2 Replies Last reply
    0
    • M magicast

      hi~ I apologize being not good at English. I wanna serialize my own data for network/file/etc... I thought that stream of c++ standard library is suitable. but, there's no facilities exactly for my use. 1. I wanna store stream data into memory.(not file) 2. insertion and extraction must operate in binary mode. // example of usage // proc A omystream ms; // mystream is that I wanted.. int n = 1; short s = 2; ms << n << s; // or ms << int_b(n) << short_b(s); // int_b(), short_b() is custom manipulators ::send(ms.buf(),...); // send to network // proc B BYTE buf[4096]; ::revc(buf,...); // receive from network imystream ms(buf, dwSizeReceived); // dwSizeReceived = 6 int n; short s; ms >> n >> s; // n = 1, s = 2 // or ms >> int_b(n) >> short_b(s); // result data occupy 6 bytes memory. (4 for n(int), 2 for s(short)) send and receive 6 byte of memory. memory layout: 00 00 00 01 00 02 // 6 byte not "000000010002" // 12 byte this mean not string but binary. how can I achieve it? derive strstream or stream? derive stream_buf? I need your advice. :) thanks in advance.

      A Offline
      A Offline
      AlexO
      wrote on last edited by
      #2

      What you are looking for is ::CreateStreamOnHGlobal. You might consider SAFEARRAY(BYTE) as well (instead of).

      1 Reply Last reply
      0
      • M magicast

        hi~ I apologize being not good at English. I wanna serialize my own data for network/file/etc... I thought that stream of c++ standard library is suitable. but, there's no facilities exactly for my use. 1. I wanna store stream data into memory.(not file) 2. insertion and extraction must operate in binary mode. // example of usage // proc A omystream ms; // mystream is that I wanted.. int n = 1; short s = 2; ms << n << s; // or ms << int_b(n) << short_b(s); // int_b(), short_b() is custom manipulators ::send(ms.buf(),...); // send to network // proc B BYTE buf[4096]; ::revc(buf,...); // receive from network imystream ms(buf, dwSizeReceived); // dwSizeReceived = 6 int n; short s; ms >> n >> s; // n = 1, s = 2 // or ms >> int_b(n) >> short_b(s); // result data occupy 6 bytes memory. (4 for n(int), 2 for s(short)) send and receive 6 byte of memory. memory layout: 00 00 00 01 00 02 // 6 byte not "000000010002" // 12 byte this mean not string but binary. how can I achieve it? derive strstream or stream? derive stream_buf? I need your advice. :) thanks in advance.

        B Offline
        B Offline
        Ben Burnett
        wrote on last edited by
        #3

        The simplest way would be to deliminate all the data. In the case of streams, it would be easiest to use spaces, as the stream's default behaviour is to skip them.

        ostringstream out;

        int n = 1;
        short s = 2;

        out << n << ' ' << s;

        // send the string over the network ( out.str () )

        istringstream in ( out.str () );

        n = s = 0;

        in >> n >> s;

        cout << "n = " << n << ", s = " << s << endl;

        cheers, -B

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups