By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,016 Members | 2,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,016 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Karl Ebener wrote:
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
Jul 22 '05 #2

P: n/a
Karl Ebener wrote in news:41***********************@newsread2.arcor-
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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3

P: n/a
Rob Williscroft wrote:
Karl Ebener wrote in news:41***********************@newsread2.arcor-
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
Jul 22 '05 #4

P: n/a
Victor Bazarov wrote in news:cOKmd.11056$Ae.10107
@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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #5

P: n/a

"Rob Williscroft" <rt*@freenet.co.uk> wrote in message
news:Xn**********************************@130.133. 1.4...
Karl Ebener wrote in news:41***********************@newsread2.arcor-
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.


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

string can handle null bytes.

john
Jul 22 '05 #6

P: n/a
John Harrison wrote in news:30*************@uni-berlin.de in comp.lang.c++:

"Rob Williscroft" <rt*@freenet.co.uk> wrote in message
news:Xn**********************************@130.133. 1.4...
Karl Ebener wrote in news:41***********************@newsread2.arcor-
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.


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

string can handle null bytes.


Duh !, smacks forehead :)

aRob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.