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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ramesh

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

    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
  • puzzlecracker

    #2
    Re: copying contents from a char array to std::string obj

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

    Comment

    • Christian Hackl

      #3
      Re: copying contents from a char array to std::string obj

      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<cha rcsMac(Mac, Mac + sizeof(Mac));


      --
      Christian Hackl

      Comment

      • Jim Langston

        #4
        Re: copying contents from a char array to std::string obj


        "Ramesh" <rramesh1@gmail .comwrote in message
        news:58cd5406-6ad1-4508-a6a7-94533c5605b3@i2 4g2000prf.googl egroups.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?


        Comment

        • James Kanze

          #5
          Re: copying contents from a char array to std::string obj

          On Jul 25, 6:42 pm, "Alf P. Steinbach" <al...@start.no wrote:

          [...]
          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:james.kan ze@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          • James Kanze

            #6
            Re: copying contents from a char array to std::string obj

            On Jul 26, 4:03 am, "Alf P. Steinbach" <al...@start.no wrote:
            * James Kanze:
            On Jul 25, 6:42 pm, "Alf P. Steinbach" <al...@start.no wrote:
            [...]
            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::da ta() 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<cha rinstead).
            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:james.kan ze@gmail.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            Working...