Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

converting char array to string type

antonsantons Posts: 117Member
Good day

Does anyone know of a function that will assign the content of a char array to a string. I know you could iterate through the array and assign the characters to the string one by one, but I don't want to make something that might exist already. I can't find anything on the net, maybe I'm just looking in the wrong place.

Thank you

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    : Good day
    :
    : Does anyone know of a function that will assign the content of a char array to a string. I know you could iterate through the array and assign the characters to the string one by one, but I don't want to make something that might exist already. I can't find anything on the net, maybe I'm just looking in the wrong place.
    :
    : Thank you
    :

    do you mean c++ std::string object? You don't have to do it one character at a time, but I suppose you could.

    Here is is hard way
    [code]
    std::string str;
    char array[] = "Hello World";
    for(int i = 0; array[i] != 0; i++)
    str += array[i];
    [/code]


    And the easy way
    [code]
    std::string str;
    char array[] = "Hello World";

    str = array;
    [/code]
  • antonsantons Posts: 117Member
    Thank you, I tried the second one, but still I have problems.
    I'm writing my first c++ program that I'm actually being paid for (very little, but the experience is great), and I have done some modules at university (distance education) in c++, and not C, so I was hoping to avoid this whole C char array nonsense, but functions that I'm reusing that has been written by other people has these as return types, so where I can I would like to convert from char array to string, then use string to do my manipulation as I like the string type more and feel comfortable with it.
    So, I'm polling a hardware panel(that has devices connected to it), and receive messages back via the serial port. For using the serial port, we have code found on the internet(codeproject). The data I receive through the serial port, (5 chars at this stage)are received in a char array. I want to assign this to a string, then do my thing. But your second example only assigns the first two chars, as the third one is zero. Which also confuses me, from my reading this morning on the net about char arrays, they are terminated by the NULL character. In C++ code this looks like '' (for some reason the quotes get screwed up when posting to this site), but on another place I now read this is equal to zero. Confusing!!
    So what if you also want to receive zeros via the char array. How can zero then be equal to the NULL character. Something here does not make sense. Please let me know if you understand me and can help clear this up. I'll also bring some books along from home after lunch.

    Thanks for now
    Anton
  • stoberstober Posts: 9,765Member ✭✭✭
    : Thank you, I tried the second one, but still I have problems.
    : I'm writing my first c++ program that I'm actually being paid for (very little, but the experience is great), and I have done some modules at university (distance education) in c++, and not C, so I was hoping to avoid this whole C char array nonsense, but functions that I'm reusing that has been written by other people has these as return types, so where I can I would like to convert from char array to string, then use string to do my manipulation as I like the string type more and feel comfortable with it.
    : So, I'm polling a hardware panel(that has devices connected to it), and receive messages back via the serial port. For using the serial port, we have code found on the internet(codeproject). The data I receive through the serial port, (5 chars at this stage)are received in a char array. I want to assign this to a string, then do my thing. But your second example only assigns the first two chars, as the third one is zero. Which also confuses me, from my reading this morning on the net about char arrays, they are terminated by the NULL character. In C++ code this looks like '' (for some reason the quotes get screwed up when posting to this site), but on another place I now read this is equal to zero. Confusing!!
    : So what if you also want to receive zeros via the char array. How can zero then be equal to the NULL character. Something here does not make sense. Please let me know if you understand me and can help clear this up. I'll also bring some books along from home after lunch.
    :
    : Thanks for now
    : Anton
    :


    [blue]You seem to be confused by the definitions. NULL is a #define term and normally #define'ed in c++ as 0. Older C compilers sometimes defined it differently.
    [code]
    #define NULL 0
    [/code]

    Use of NULL is discouraged in C++ because it can be redefined to be anything one wants -- c++ standards do not dictate what NULL should be.

    The '' and 0 are one in the same thing. The compiler will translate '' to 0 during compilation.

    All C-style strings are said to be NULL-terminated -- that definition is carry-over from C language. It really means that the end of the string is indicated by the byte which contains a 0.

    you cannot assign C-strings that have enbedded 0s to std::string as I posted earlier. As you found out the assignment stops at the first 0. You could do it one character at a time, but then std::string is no longer an ascii string but a binary string, and most of the std::string methods cannot be used, again because of embedded 0s.

    In this example, the output of the first cout is jest "Hello" because of the embedded 0.
    [code]
    #include
    #include
    using namespace std;

    int main()
    {
    int i;
    char str[] = "Hello World";
    string s = str;
    cout << s << endl; [red]<< output = "Hello"[/red]
    int sz = sizeof(str);
    s = "";
    for(i = 0; i < sz; i++)
    s += str[i];

    cout << s << endl;
    [red]// now assign characters one at a time[/red]
    sz = s.length();
    for(i = 0; i < sz; i++)
    cout << s[i];
    cout << endl; [red]output = "Hello World" [/red]
    return 0;
    }
    [/code]



  • antonsantons Posts: 117Member
    Thank you for your effort. You should be paid money!!
    My boss also had a go at it. Zero apparently has value 40 something (or whatever) on the ASCII table, and NULL has the value zero on the ASCII table. The education I got kind of left me hanging a bit in so far as preparing me for C++ programming in the real world, so now and again I find out that I don't know what I thought I knew.
    For now the boss says I could try the ASSIGN member of the string class. You can specify how long the char array is that is to be copied (the length info is returned via the SERIAL class we are using), so the zeros in the message should not be a problem.
    I'll let you know what the outcome is, after lunch.
    Thank you once again, and sorry for my incompetence.
    Regards
  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2005-7-22 5:34:45[/red][/b][hr]
    : Thank you for your effort. You should be paid money!!
    : My boss also had a go at it. Zero apparently has value 40 something (or whatever) on the ASCII table,
    [blue]No -- 48 on the ASCII table is the character '0', not the binary number 0, they are not the same thing.
    http://www.lookuptables.com/
    [/blue]


    :and NULL has the value zero on the ASCII table.
    [blue]Wrong again. The "NUL" in the ASCII table is not the same as NULL we were talking about in earlier posts. sometimes NULL can be defined like this in C language, which is clearly not the same as the NUL you saw in the ascii table. One (NULL) is a #define while the other isn't.[/blue]
    [code]
    #define NULL (char far *)0
    [/code]

    The education I got kind of left me hanging a bit in so far as preparing me for C++ programming in the real world, so now and again I find out that I don't know what I thought I knew.
    [blue]Welcome to the real-world! That happens to everyone. Universities and colleges only teach the language. The degree only gives you a few tools to begin learning. Now that you have a real job, you can start learning how things really work[/blue]

    : For now the boss says I could try the ASSIGN member of the string class. You can specify how long the char array is that is to be copied (the length info is returned via the SERIAL class we are using), so the zeros in the message should not be a problem.
    [blue]Yes, that will work too[/blue]
    [code]
    int main()
    {
    int i;
    string s;
    char str[] = "Hello World";
    int sz = sizeof(str);
    s.assign(str,sz);
    sz = s.length();
    for(i = 0; i < sz; i++)
    cout << s[i];
    cout << endl;
    return 0;
    }
    [/code]
    : I'll let you know what the outcome is, after lunch.
    [blue]Its only 7:00 a.m. on my side of the world :-)[/blue]



  • antonsantons Posts: 117Member
    It seems APPEND will do for now. I will try and get to grips with this NUL and NULL and ZERO story, but maybe not today. (the boss comes and goes).
    On another subject, and I know it is none of my business, but why do you (STOBER that is) reply to the posts like your life depended on it. It must be frustrating when peolple are kind of asking similar questions all the time, and you I suppose you must sometimes spend a great deal of time helping people (who I think dont always deserve it, myself included), while you have work of your own to do as well.
    Why do you do it?? Please tell.
  • stoberstober Posts: 9,765Member ✭✭✭
    : Why do you do it?? Please tell.
    :

    Hobby. :-)
Sign In or Register to comment.