473,387 Members | 1,721 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,387 software developers and data experts.

istrstream, simple but????

Can istrstream contain binary data like '\0' and other non ascii characters????

My program received a byte array through jni (java) and i must put it in a stream to continue the flow in the program, but i am unable to succeed this STL problem. Below is a little sample that should show my problem...

const char* data1 = "hej1";
const char* data2 = "hej2";
char data[10];
memcpy( &data[0], data1, 5 );
memcpy( &data[5], data2, 5 );
int len = 10;
std::istrstream isb( data );
std::ostrstream osb;
osb << isb;

printf( "len = %i\n", osb.pcount() );
printf( "str = %s\n", osb.str() );
Output is:
len = 8 (where does it get this number from?????)
str = 0012FE24????????????????????????²²²²??????

I expected:
len = 10
str = hej1

Are there any solution to my fustrations????
--
Regards, Frank Nielsen

- En ægte Karl Koder -
Jul 22 '05 #1
8 2011

"Frank Nielsen" <"Frank Nielsen"> wrote in message
news:40*********************@dread11.news.tele.dk. ..
Can istrstream contain binary data like '\0' and other non ascii characters????

istrstream cannot contain '\0'.

My program received a byte array through jni (java) and i must put it in a stream to continue the flow in the program, but i am unable to succeed this
STL problem. Below is a little sample that should show my problem...
const char* data1 = "hej1";
const char* data2 = "hej2";
char data[10];
memcpy( &data[0], data1, 5 );
memcpy( &data[5], data2, 5 );
int len = 10;
std::istrstream isb( data );
std::ostrstream osb;
osb << isb;

printf( "len = %i\n", osb.pcount() );
printf( "str = %s\n", osb.str() );
Output is:
len = 8 (where does it get this number from?????)
str = 0012FE24????????????????????????²²²²??????

I expected:
len = 10
str = hej1

Are there any solution to my fustrations????


Use istringstream.

john
Jul 22 '05 #2
John Harrison wrote:
"Frank Nielsen" <"Frank Nielsen"> wrote in message
news:40*********************@dread11.news.tele.dk. ..
Can istrstream contain binary data like '\0' and other non ascii


characters????

istrstream cannot contain '\0'.

My program received a byte array through jni (java) and i must put it in a


stream to continue the flow in the program, but i am unable to succeed this
STL problem. Below is a little sample that should show my problem...


const char* data1 = "hej1";
const char* data2 = "hej2";
char data[10];
memcpy( &data[0], data1, 5 );
memcpy( &data[5], data2, 5 );
int len = 10;
std::istrstream isb( data );
std::ostrstream osb;
osb << isb;

printf( "len = %i\n", osb.pcount() );
printf( "str = %s\n", osb.str() );
Output is:
len = 8 (where does it get this number from?????)
str = 0012FE24????????????????????????²²²²??????

I expected:
len = 10
str = hej1

Are there any solution to my fustrations????

Use istringstream.

john

Thanks, but what do i do then????

Is there no way to transform a byte array into a stream???

--
Regards, Frank Nielsen

- En ægte Karl Koder -
Jul 22 '05 #3
> >>
Are there any solution to my fustrations????

Use istringstream.

john

Thanks, but what do i do then????

Is there no way to transform a byte array into a stream???


Of course there is

string str(byte_array, byte_array + byte_array_size);
istringstream stream(str);

john
Jul 22 '05 #4

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c7************@ID-196037.news.uni-berlin.de...
>
>Are there any solution to my fustrations????
Use istringstream.

john

Thanks, but what do i do then????

Is there no way to transform a byte array into a stream???


Of course there is

string str(byte_array, byte_array + byte_array_size);
istringstream stream(str);

john


Or even simpler

istringstream stream(string(byte_array, byte_array + byte_array_size));

john

Jul 22 '05 #5
John Harrison wrote:
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c7************@ID-196037.news.uni-berlin.de...
>Are there any solution to my fustrations????
Use istringstream.

john

Thanks, but what do i do then????

Is there no way to transform a byte array into a stream???


Of course there is

string str(byte_array, byte_array + byte_array_size);
istringstream stream(str);

john

Or even simpler

istringstream stream(string(byte_array, byte_array + byte_array_size));

john

Thanks, i will try it out...

What is the difference between istrstream and istringstream?

--
Regards, Frank Nielsen

- En ægte Karl Koder -
Jul 22 '05 #6
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c7************@ID-196037.news.uni-berlin.de...
"Frank Nielsen" <"Frank Nielsen"> wrote in message
news:40*********************@dread11.news.tele.dk. ..
Can istrstream contain binary data like '\0' and other non ascii

characters????

istrstream cannot contain '\0'.


Nonsense. You're confusing the conventional use of NUL as an
end-of-sequence signal with a structural limitation of
strstreambuf. Think of "abc\0d", which is still an array of
six characters even though strlen would say it's a string with
three characters before the terminating NUL.

strstream can store arbitrary binary data, provided you determine
the length of its controlled sequence properly.

Having said that, I agree that it is usually safer and easier
to use the facilities of <sstream> instead of <strstream>.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 22 '05 #7
> >

Thanks, i will try it out...

What is the difference between istrstream and istringstream?


istrstream is based upon a null terminated character array, istringstream is
based upon a string.

Incidentally your original code was wrong in a couple of places, chiefly
here

osb << isb;

looks like you thought that would copy isb to osb, but the way to do that is

osb << isb.rdbuf();

What you did was convert the isb to a void* (every stream has this implicit
conversion) and output that pointer to osb, that's where the eight bytes
came from.

john
Jul 22 '05 #8

"P.J. Plauger" <pj*@dinkumware.com> wrote in message
news:8S********************@nwrddc01.gnilink.net.. .
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c7************@ID-196037.news.uni-berlin.de...
"Frank Nielsen" <"Frank Nielsen"> wrote in message
news:40*********************@dread11.news.tele.dk. ..
Can istrstream contain binary data like '\0' and other non ascii

characters????

istrstream cannot contain '\0'.


Nonsense. You're confusing the conventional use of NUL as an
end-of-sequence signal with a structural limitation of
strstreambuf.


Yes, here's the OP's code fixed (apart from the memory leak) so that it
outputs a length of 10 bytes

#include <strstream>
#include <cstdio>
using namespace std;
int main()
{
const char* data1 = "hej1";
const char* data2 = "hej2";
char data[10];
memcpy( &data[0], data1, 5 );
memcpy( &data[5], data2, 5 );
int len = 10;
std::istrstream isb( data, 10 );
std::ostrstream osb;
osb << isb.rdbuf();
printf( "len = %i\n", osb.pcount() );
printf( "str = %s\n", osb.str() );
}

john
Jul 22 '05 #9

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

Similar topics

2
by: delisonews | last post by:
I'm looking for a simple, filesystem-based message board. (No MySQL!) Something that I could include easily in my code: include '../inc/messageboard.php'; .... so that the board shows up at...
6
by: Ahmed Ossman | last post by:
Hi All, I have this piece of code: char str; ...... double x; istrstream in(str); in >> x;
3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
8
by: Dan | last post by:
Using XML::Simple in perl is extreemly slow to parse big XML files (can be up to 250M, taking ~1h). How can I increase my performance / reduce my memory usage? Is SAX the way forward?
1
by: A. Farber | last post by:
Hi, I'm trying to port a bigger program from Visual C++ to Linux and after rewriting few Visual C++ functions (strnicmp, filelength, findnext, etc.) I'm finally down to just one (!) error...
7
by: abcd | last post by:
I am trying to set up client machine and investigatging which .net components are missing to run aspx page. I have a simple aspx page which just has "hello world" printed.... When I request...
4
by: Johannes Zellner | last post by:
Hi, how can I extract the whole rest of an istream? (All characters which have not been consumed yet?) -- Johannes
14
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
10
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.