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 6 4083
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
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/
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
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/
"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
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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
?
//...
|
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...
|
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
*...
|
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",...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |