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

copying contents from a char array to std::string obj

P: n/a
Hi.

Assuming I have a code snippet like below:

#include <iostream>
using namespace std;

char Mac[6] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5 };
std::string csMac;

1. Whats the best way to do a deep copy from the c-style char array to
the c++ string object.

I know the other way around to assign to the string object (to use a
c_str() on the string).

Thanks
Ramesh
Jul 24 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Jul 23, 10:50 pm, Ramesh <rrame...@gmail.comwrote:
Hi.

Assuming I have a code snippet like below:

#include <iostream>
using namespace std;

char Mac[6] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5 };
std::string csMac;

1. Whats the best way to do a deep copy from the c-style char array to
the c++ string object.

I know the other way around to assign to the string object (to use a
c_str() on the string).

Thanks
Ramesh
How about passing it to a string copy ctor?

string str(Mac);
Jul 24 '08 #2

P: n/a
puzzlecracker wrote:
On Jul 23, 10:50 pm, Ramesh <rrame...@gmail.comwrote:
>>
Assuming I have a code snippet like below:

#include <iostream>
using namespace std;

char Mac[6] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5 };
std::string csMac;

1. Whats the best way to do a deep copy from the c-style char array to
the c++ string object.

How about passing it to a string copy ctor?

string str(Mac);
This won't work, because the first element of Mac is '\0'. Constructing
it like this will result in an empty string.

Perhaps std::string is just the wrong data type for the OP's problem.
What about some container, say std::vector, of char?

char Mac[6] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5 };
std::vector<charcsMac(Mac, Mac + sizeof(Mac));
--
Christian Hackl
Jul 24 '08 #3

P: n/a

"Ramesh" <rr******@gmail.comwrote in message
news:58**********************************@i24g2000 prf.googlegroups.com...
Hi.

Assuming I have a code snippet like below:

#include <iostream>
using namespace std;

char Mac[6] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5 };
std::string csMac;

1. Whats the best way to do a deep copy from the c-style char array to
the c++ string object.

I know the other way around to assign to the string object (to use a
c_str() on the string).
The output of the following program is:
0 1 2 3 4 5

#include <iostream>
#include <string>

int main()
{
char Mac[6] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5 };
std::string csMac( Mac, sizeof( Mac ) );

for ( size_t i = 0; i < csMac.size(); ++i )
std::cout << static_cast<int>( csMac[i] ) << " ";
std::cout << "\n";
}

Instead of sizeof( Mac ) you could use 6, etc...

Is this what you were looking for?
Jul 24 '08 #4

P: n/a
On Jul 25, 6:42 pm, "Alf P. Steinbach" <al...@start.nowrote:

[...]
Yes, modulo terminology that's correct, and I wrote that in my
first response. In case you misunderstood that, here's a
reworded version: c_str() is not the opposite of a conversion
of your data to std::string, because c_str(), as you mention,
appends a null-byte, and there's no such in your original
data, and because your original data contains a zero byte
which can't appear within a proper zero-termianted string
(only at the end of it). The closest you get to the "the other
way" is &s[0].
What's wrong with s.data() (using it with s.size(), of course)?
(And of course, &s[0] isn't yet formally guaranteed, although it
will be, and does work with all known implementations.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 25 '08 #5

P: n/a
On Jul 26, 4:03 am, "Alf P. Steinbach" <al...@start.nowrote:
* James Kanze:
On Jul 25, 6:42 pm, "Alf P. Steinbach" <al...@start.nowrote:
[...]
Yes, modulo terminology that's correct, and I wrote that in my
first response. In case you misunderstood that, here's a
reworded version: c_str() is not the opposite of a conversion
of your data to std::string, because c_str(), as you mention,
appends a null-byte, and there's no such in your original
data, and because your original data contains a zero byte
which can't appear within a proper zero-termianted string
(only at the end of it). The closest you get to the "the other
way" is &s[0].
What's wrong with s.data() (using it with s.size(), of course)?
(And of course, &s[0] isn't yet formally guaranteed, although it
will be, and does work with all known implementations.)
It's more to write,
But easier, since you don't have to stretch your fingers for any
unusual characters. With a French or German keyboard (which
don't have a [ or a ]), you even need to use some implementation
dependent means of entering it:-). And data() expresses the
intent much better.
may be just a wrapper for c_str(), and IIRC is lacking in
const department.
In which way? In the current standard, it is a const function
which returns a const char* (and of course, in the current
standard, any use of &s[0] to access anything but the first
character is undefined behavior). In the next version of the
standard (which will guarantee contiguity), there will be both a
const and a non-const version (and the other container which
guarantees contiguous-ness, std::vector, will also have a const
and a non-const data() function).
But mainly, it's just non-idiomatic.
In what way? I regularly use std::string::data() when that's
what I need (as opposed to a null terminated string---of
course, most of the time, when I need something like that, I'll
be useing an std::vector<charinstead).
I guess, since it has no clear advantage for anything, people
just don't use it.
Except that people do. I've seen s.data() a lot more than
&s[0]. It wasn't clear until very recently that &s[0] was in
fact reliable, since people were always talking about some
(mythical) implementation where the actual data wasn't
contiguous.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 26 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.