istrstream, simple but????

F

Frank Nielsen

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::eek:strstream 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????
 
J

John Harrison

Frank Nielsen said:
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::eek:strstream 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
 
F

Frank Nielsen

John said:
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::eek:strstream 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???
 
J

John Harrison

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
 
J

John Harrison

John Harrison said:
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
 
F

Frank Nielsen

John said:
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?
 
P

P.J. Plauger

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
 
J

John Harrison

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
 
J

John Harrison

P.J. Plauger said:
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::eek:strstream osb;
osb << isb.rdbuf();
printf( "len = %i\n", osb.pcount() );
printf( "str = %s\n", osb.str() );
}

john
 

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

Forum statistics

Threads
474,167
Messages
2,570,913
Members
47,455
Latest member
Delilah Code

Latest Threads

Top