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

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

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
6 4076
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
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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Danny Anderson | last post by:
Hola! I am working on a program where I am including a library that came with my numerical methods textbook. The "util.h" simply includes a large number of files. I had to change the util.h...
1
by: Matt Garman | last post by:
What is the "best" way to copy a vector of strings to an array of character strings? By "best", I mean most elegantly/tersely written, but without any sacrifice in performance. I'm writing an...
37
by: Zombie | last post by:
Hi, what is the correct way of converting contents of a <string> to lowercase? There are no methods of <string> class to do this so I fallback on strlwr(). But the c_str() method returns a const...
17
by: Karl Ebener | last post by:
Hi! I asked a similar question before but then changed everything to using char-Arrays instead of the string class, but I would rather not do this again. So, does anyone know of a...
2
by: ehui928 | last post by:
hi, everybody I am a newbie in STL. When I compile the following program under gcc4.0, I got a the following errors. I wonder whether the form of list< vector<string> > is correct in STL ? //...
5
by: Fred | last post by:
Hi: I've got the following request: (suppose the required head file is included) vector<stringvec; // .......... // push some items into vec string str; // here I want to copy some range...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
6
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1",...
42
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...

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.