473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

ostringstream and memcpy

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

Jan 18 '06 #1
3 5291
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
Jan 18 '06 #2
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()[i]));
}
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

Jan 19 '06 #3
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.
Jan 19 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alex Vinokur | last post by:
Is it possible to use vector<ostringstream> ? Here is what I have got. =========================================== Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927...
6
by: Eric Boutin | last post by:
Hi ! I have a strange problem with a std::ostringstream.. code : #include <sstream> /*...*/ std::ostringstream ss(); ss << "\"\"" << libpath << "\"\" \"\"" << argfilename << "\"\"...
5
by: Als | last post by:
What's the meaning of "freeze" as member of ostringstream class? Is it needed always to call freeze() at the disposal of an ostringstream object? Is it also needed before disposing an istringsteam...
5
by: Simon Pryor | last post by:
I am having some strange problems using std::ostringstream. The simple stuff works okay, but trying to use: std::ostringstream::str(const std::string&) or:...
3
by: Mathieu Malaterre | last post by:
Hello, I am trying to write this simple code: std::ostringstream s; s << 1024; std::cout << s.str() << std::endl; s.str(""); // <- problem s << 512; std::cout << s.str() << std::endl;
3
by: Kyle Kolander | last post by:
I posted this in response to a previous thread but have not gotten any replies back... Hopefully someone has an answer? From looking at sections 27.7.3.2 and 27.7.1.2 of the standard, it appears...
3
by: Bob Altman | last post by:
Hi all, Why doesn't the following unmanaged C++ code work as expected: string s; ostringstream strm(s); // This stream should store results in s strm << 25; cout << s << endl; // s...
3
by: Generic Usenet Account | last post by:
With the deprecated ostrstream class, when the constructor is invoked without arguments, memory is dynamically allocated. In that case the onus on freeing the memory lies with the user. Typically...
11
by: coomberjones | last post by:
I have a few std::strings that I am using to store raw binary data, each of which may very well include null bytes at any point or points. I want to slap them together into a single string, so I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.