ostringstream and memcpy

A

Alex Vinokur

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?
 
V

Victor Bazarov

Alex said:
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
 
A

Alex Vinokur

Victor said:
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
 
I

Ian Collins

Alex said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top