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

R

Ramesh

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
 
P

puzzlecracker

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);
 
C

Christian Hackl

puzzlecracker said:
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<char> csMac(Mac, Mac + sizeof(Mac));
 
J

Jim Langston

Ramesh said:
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 ) << " ";
std::cout << "\n";
}

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

Is this what you were looking for?
 
R

Ramesh

Thanks,

I dont think I need a zero terminated string to use c_str(), as per
the reference pages of c_str, it says:

"Generates a null-terminated sequence of characters (c-string) with
the same content as the string object and returns it as a pointer to
an array of characters"

A terminating null character is automatically appended.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Regards
Ramesh

* Ramesh:
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.

"best" is not meaningful without some criteria.

And as Christian Hackl remarked else-thread you're probably better off using
some other container type, not a string.

One way to initialize a std::string from the data is

     #include <iostream>
     #include <string>

     template< typename T, size_t N >
     T const* begin( T const (&a)[N] ) { return a; }

     template< typename T, size_t N >
     T const* end( T const (&a)[N] ) { return a + N; }

     int main()
     {
         using namespace std;

         static char const macValues[6] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5 };
         std::string const mac( begin(macValues), end(macValues) );

         for( size_t i = 0; i < mac.length(); ++i )
         {
             cout << (int) mac << ' ';
         }
         cout << endl;
     }
I know the other way around to assign to the string object (to use a
c_str() on the string).

That's not "the other way", that's a conversion to pointer to zero-terminated
string. You don't have a zero-terminated string in the first place.

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
R

Ramesh

Alf:

In your previous post you had mentioned that I need a null terminated
string to use c_str(), which is not correct.

I hope that clarifies.
 
R

Ramesh

* Ramesh:




Just ask when you fail to understand something: don't make false assertions.

Also, please don't top-post and please don't quote extraneous material, and
please do read the FAQ before posting further articles.

TIA.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Alf:

Sorry about the top-post, will pay attention to that next time. I dont
want to make any assertions.

bottomline: the code I posted works fine with cstr usage, which, in
your first response you had mentioned it needs a null terminated
string, just wanted to clarify that cstr appends a NULL automatically
to the target string even if the source doesnt have one.
 
J

James Kanze

[...]
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.)
 
J

James Kanze

* James Kanze:
[...]
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 said:
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.
 

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

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top