<string>: Handling of "\0" and copy into char x[];

K

Karl Ebener

Hi!

I am currently using a string to hold data (can be strings as well as
binary!). Now it occured to me, that the <string> might be unable to
handle Null-Bytes. Is that so?

Following question: How can I copy the binary data of a string into an
array, such as

char mtext[512] (or alike) ?

Until now I used c_str() to get a pointer to a c_str-representation of
my string and copied this with strncopy. But there I definitely lose
Null-Bytes.
I have to convert the other way, too? I just used a

string s = mtext;

for this until now.

Should I use something else for holding the data or is there a way to
copy the bytes "as they are" into the array and back?

Tnx a lot in advance.
Karl
 
V

Victor Bazarov

Karl said:
I am currently using a string to hold data (can be strings as well as
binary!). Now it occured to me, that the <string> might be unable to
handle Null-Bytes. Is that so?
No.

Following question: How can I copy the binary data of a string into an
array, such as

char mtext[512] (or alike) ?

By using memcpy, I suppose.
Until now I used c_str() to get a pointer to a c_str-representation of
my string and copied this with strncopy. But there I definitely lose
Null-Bytes.

Of course. You're asking a C++ string to give you a C string. By the
definition C strings _end_ with null characters (IOW, C strings are the
ones that are "unable to handle" null chars, but only if you use string
functions to manage them).
I have to convert the other way, too? I just used a

string s = mtext;

for this until now.

Should I use something else for holding the data or is there a way to
copy the bytes "as they are" into the array and back?

Maybe. Try std::vector<char>.

Or, just involve the size into the operation:

memcpy(mtext + someoffset, somestring.data(), somestring.size());

and

somestring.assign(mtext, mtext + whatever_size);

Just make sure you don't step over the boundaries of your char array.

V
 
R

Rob Williscroft

Karl Ebener wrote in online.net in comp.lang.c++:
Hi!

I am currently using a string to hold data (can be strings as well as
binary!). Now it occured to me, that the <string> might be unable to
handle Null-Bytes. Is that so?
Yes.


Following question: How can I copy the binary data of a string into an
array, such as

char mtext[512] (or alike) ?

#include <iostream>
#include <algorithm>
#include <string>
#include <cassert>

char mtext[512];

void string_to_mtext( std::string const &s )
{
assert( s.size() <= sizeof( mtext ) );
std::copy( s.begin(), s.end(), mtext );
}


int main()
{
using namespace std;

string s = "sample";

string_to_mtext( s );

mtext[ s.size() ] = 0; /* for next line */
cout << mtext << '\n';


s = ""; /* for testing next line */
s.assign( mtext, mtext + sizeof( mtext ) );

cout << s << '\n';
}

Until now I used c_str() to get a pointer to a c_str-representation of
my string and copied this with strncopy. But there I definitely lose
Null-Bytes.
I have to convert the other way, too? I just used a

string s = mtext;

string has an iterator constructor too:

string s( mtext, mtext + sizeof( mtext ) );

Note:

sizeof( mtext ) above is the count of char's as sizeof( char ) == 1.

But convert your programme to use wchar_t and std::wstring then
you'll need to divide by sizeof( wchar_t ).

Rob.
 
V

Victor Bazarov

Rob said:
Karl Ebener wrote in online.net in comp.lang.c++:

Hi!

I am currently using a string to hold data (can be strings as well as
binary!). Now it occured to me, that the <string> might be unable to
handle Null-Bytes. Is that so?

Yes.


Following question: How can I copy the binary data of a string into an
array, such as

char mtext[512] (or alike) ?


#include <iostream>
#include <algorithm>
#include <string>
#include <cassert>

char mtext[512];

void string_to_mtext( std::string const &s )
{
assert( s.size() <= sizeof( mtext ) );
std::copy( s.begin(), s.end(), mtext );
}


int main()
{
using namespace std;

string s = "sample";

string_to_mtext( s );

mtext[ s.size() ] = 0; /* for next line */
cout << mtext << '\n';


s = ""; /* for testing next line */
s.assign( mtext, mtext + sizeof( mtext ) );

cout << s << '\n';
}


Until now I used c_str() to get a pointer to a c_str-representation of
my string and copied this with strncopy. But there I definitely lose
Null-Bytes.
I have to convert the other way, too? I just used a

string s = mtext;


string has an iterator constructor too:

string s( mtext, mtext + sizeof( mtext ) );

Note:

sizeof( mtext ) above is the count of char's as sizeof( char ) == 1.

But convert your programme to use wchar_t and std::wstring then
you'll need to divide by sizeof( wchar_t ).

That's why it would be preferrable (and more generic) to say

typedef std::string mystring;
...
mystring s( mtext, mtext + sizeof(mtext) / sizeof(mtext[0]) );

Costs nothing and saves a headache.

V
 
R

Rob Williscroft

Victor Bazarov wrote in @newsread1.dllstx09.us.to.verio.net in comp.lang.c++:
Note:

sizeof( mtext ) above is the count of char's as sizeof( char ) == 1.

But convert your programme to use wchar_t and std::wstring then
you'll need to divide by sizeof( wchar_t ).

That's why it would be preferrable (and more generic) to say

typedef std::string mystring;
...
mystring s( mtext, mtext + sizeof(mtext) / sizeof(mtext[0]) );

Costs nothing and saves a headache.

IRL I'd probably give mtext a #define'd or constant expression
size and just use that.

std::size_t const mtext_size = 512;

char mtext[ mtext_size ];

etc ...

Rob.
 
J

John Harrison

Rob Williscroft said:
Karl Ebener wrote in online.net in comp.lang.c++:


Yes.

'Might be *unable* to handle null bytes'. The answer is no.

string can handle null bytes.

john
 
R

Rob Williscroft

John Harrison wrote in in comp.lang.c++:
'Might be *unable* to handle null bytes'. The answer is no.

string can handle null bytes.

Duh !, smacks forehead :)

aRob.
 

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
473,776
Messages
2,569,603
Members
45,191
Latest member
BuyKetoBeez

Latest Threads

Top