ostringstream and memcpy

Discussion in 'C++' started by Alex Vinokur, Jan 18, 2006.

  1. Alex Vinokur

    Alex Vinokur Guest

    typedef unsigned short u_short;
    typedef unsigned char u_char;

    u_short us1 = 0xabcd; // or u_short us1 = htons (0xabcd);
    u_short us2 = 0xef; // or u_short us2= htons (0xef);

    // ----- memcpy ---
    u_char* buf = new (nothrow) u_char [512];
    // Checking buf != 0
    memcpy (&buf[0], (u_char*)us1, sizeof (us1));
    memcpy (&buf[sizeof (us1), (u_char*)us2 sizeof (us2);

    It works fine.
    Now 'buf' contains the following bytes: ab-cd-00-ef

    // ----- ostringstream ---
    ostringstream oss;
    oss << hex << setw (sizeof (u1) * 2) << setfill ((char*)0) <<
    (u_char*) &us1
    << setw (sizeof (u2) * 2) << setfill ((char*)0) <<
    (u_char*) &us2;

    Of course, it doesn't work as memcpy, i.e. oss.str() doesn't contain
    ab-cd-00-ef
    It is because 'operator <<' is not memory-related operator, but
    string-related one.


    // ------------------------------

    Nevertheless, is it possible to do via ostringstream (or via other
    feature of C++) what we do via memcpy?


    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Jan 18, 2006
    #1
    1. Advertising

  2. Alex Vinokur wrote:
    > typedef unsigned short u_short;
    > typedef unsigned char u_char;
    >
    > u_short us1 = 0xabcd; // or u_short us1 = htons (0xabcd);
    > u_short us2 = 0xef; // or u_short us2= htons (0xef);
    >
    > // ----- memcpy ---
    > u_char* buf = new (nothrow) u_char [512];
    > // Checking buf != 0
    > memcpy (&buf[0], (u_char*)us1, sizeof (us1));
    > memcpy (&buf[sizeof (us1), (u_char*)us2 sizeof (us2);
    >
    > It works fine.
    > Now 'buf' contains the following bytes: ab-cd-00-ef
    >
    > // ----- ostringstream ---
    > ostringstream oss;
    > oss << hex << setw (sizeof (u1) * 2) << setfill ((char*)0) <<
    > (u_char*) &us1
    > << setw (sizeof (u2) * 2) << setfill ((char*)0) <<
    > (u_char*) &us2;
    >
    > Of course, it doesn't work as memcpy, i.e. oss.str() doesn't contain
    > ab-cd-00-ef
    > It is because 'operator <<' is not memory-related operator, but
    > string-related one.
    >
    >
    > // ------------------------------
    >
    > Nevertheless, is it possible to do via ostringstream (or via other
    > feature of C++) what we do via memcpy?



    Have you tried using 'write' instead of <<?

    V
     
    Victor Bazarov, Jan 18, 2006
    #2
    1. Advertising

  3. Alex Vinokur

    Alex Vinokur Guest

    Victor Bazarov wrote:
    > Alex Vinokur wrote:

    [snip]
    > > Nevertheless, is it possible to do via ostringstream (or via other
    > > feature of C++) what we do via memcpy?

    >
    >
    > Have you tried using 'write' instead of <<?

    [snip]

    Indeed. I forgot.
    Thanks.

    Here is a sample.


    =========================================
    Microsoft Windowsw XP Professional
    Microsoft C++ Compiler Version 13.10.3077
    =========================================

    ------ C++ code : file foo.cpp ------
    #include <iostream>
    #include <iomanip>
    #include <sstream>
    #include <winsock2.h> // for htons() and htonl(), because of
    Microsoft's little endian
    using namespace std;

    typedef unsigned short ushort;
    typedef unsigned long ulong;
    typedef unsigned char uchar;

    int main( )
    {
    ushort us1 = htons(0xabcd);
    ushort us2 = htons(0xef);
    ulong ul1 = htonl(0x1256);
    ulong ul2 = htonl(0x34);
    ulong ul3 = htonl(0xa1b2c3d4);

    stringstream ss;

    ss.write((char*)&us1, sizeof (us1));
    ss.write((char*)&us2, sizeof (us2));
    ss.write((char*)&ul1, sizeof (ul1));
    ss.write((char*)&ul2, sizeof (ul2));
    ss.write((char*)&ul3, sizeof (ul3));

    cout << hex;
    for (int i = 0; i < ss.str().size(); i++)
    {
    cout << setw (2) << setfill ('0') <<
    static_cast<ushort>(static_cast<uchar>(ss.str()));
    }
    cout << dec;
    cout << endl;


    return 0;
    }
    -------------------------------------



    ------ Running ------

    $ ./foo.exe

    abcd00ef0000125600000034a1b2c3d4

    --------------------


    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Jan 19, 2006
    #3
  4. Alex Vinokur

    Ian Collins Guest

    Alex Vinokur wrote:
    >
    > typedef unsigned short ushort;
    > typedef unsigned long ulong;
    > typedef unsigned char uchar;
    >

    Do VC++/windows headers have the C99 size types (uint16_t and friends)?

    Saves all these duplicate typedefs.

    --
    Ian Collins.
     
    Ian Collins, Jan 19, 2006
    #4
    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. Voronkov Konstantin
    Replies:
    1
    Views:
    742
    Voronkov Konstantin
    Nov 18, 2005
  2. Replies:
    10
    Views:
    964
  3. Olaf van der Spek

    std::ostringstream and \r\n

    Olaf van der Spek, May 3, 2006, in forum: C++
    Replies:
    4
    Views:
    525
    Old Wolf
    May 5, 2006
  4. Replies:
    3
    Views:
    372
  5. Adam Nielsen
    Replies:
    8
    Views:
    1,223
    James Kanze
    Oct 17, 2007
Loading...

Share This Page