Convert Char variables to string C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wackieboy
    New Member
    • Apr 2013
    • 4

    Convert Char variables to string C++

    I need to find some sort of method to convert a series of char variables to a string, to be shown in a label. I've searched for two days and experimented myself just as long, and the closest I've gotten simply puts ASCII values into the string with the following command:

    Code:
    label1 -> Text = System::Convert::ToString(fdp8), System::Convert::ToString(fdp7), System::Convert::ToString(fdp6), System::Convert::ToString(fdp5), System::Convert::ToString(fdp4), System::Convert::ToString(fdp3), System::Convert::ToString(fdp2), System::Convert::ToString(fdp1);
    fdp1-fdp8 are all char variables.
    Thanks
    Last edited by wackieboy; Apr 17 '13, 04:18 PM. Reason: Code had been displayed as text
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Use a stringstream;

    Code:
    string str;
    char var = 123;
    stringstream ss;
    
    ss << var;
    ss >> str;
    You should see a { which is the ASCII symbol for 123.

    If you actually need 123 in the string as three characters, then assign var to a temporary int variable:
    Code:
    string str;
    char var = 123;
    stringstream ss;
    int temp = var;
    ss << temp;
    ss >> str;

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      And I now notice that your string is String, which is C# and not C++. I may have given an answer in the wrong language.

      So is it C# or not?

      Comment

      • wackieboy
        New Member
        • Apr 2013
        • 4

        #4
        It's C++, though I don't have the code available to test it. Also, what would the syntax be to put multiple char variables into the string/stringstream together in one command? Also, I'm fairly new to programming overall, and I'm not entirely sure what you meant about string being String. Thanks for the reply

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          The names of things in C++ are case-sensitive. So "string" is different from "String". "string" is part of the C++ standard library whereas "String" is not C++ at all but is part of the C# library. They work completely different.

          Also "System" , as seen in your errors, is C# and not C++.

          If you are asserting you are using C++, then do you mean "managed C++" that's promoted by Microsoft? This would be using Microsoft Visual Studio and creating C++ projects that have CLR support. The CLR is C#. These projects use both C++ and C# at the same time.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Strictly String is part of the .NET library not the C# library it is just that C# uses .NET.

            Other languages that can use .NET are VB, J# C++ (and a whole host of other ones I can't remember/be bothered to name).

            So yes String might normally be associated with C# but it is actually .NET which means you could be using it from a C++/CLR program.

            It is however hard to mix CLI objects (like String) with native objects (like stringstream).

            Comment

            • wackieboy
              New Member
              • Apr 2013
              • 4

              #7
              Yes it's with visual C++, so the combination of the two I guess

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Well, now I am re-reading your original post where you say all that happens is the ASCII values of your Char variables are all that is in the String.

                The ASCII values are all that is supposed to be in the String. What else were you anticipating?
                Last edited by weaknessforcats; Apr 19 '13, 05:15 PM. Reason: typo

                Comment

                • wackieboy
                  New Member
                  • Apr 2013
                  • 4

                  #9
                  Instead of showing actual characters in the label, it just shows the ASCII value of the first character.

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Like this:

                    Code:
                    String str = "label = ";
                                char a = 'A';
                                char b = 'B';
                                str += a;
                                str += b;
                                Console.WriteLine(str);
                    ?

                    I didn't see anything in your code snippet about appending the char variales together.

                    Comment

                    • seeulata
                      New Member
                      • May 2013
                      • 1

                      #11
                      I heard of a string function "atoi" I think. Also, "itoa".

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        atoi and itoa are archaic C functions. This thread is Managed C++.

                        Comment

                        • Banfa
                          Recognized Expert Expert
                          • Feb 2006
                          • 9067

                          #13
                          Actually itoa isn't even a C standard library function. It is an extension in some platforms is all.

                          Comment

                          Working...