473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. 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
Apr 17 '13 #1
12 4887
weaknessforcats
9,208 Expert Mod 8TB
Use a stringstream;

Expand|Select|Wrap|Line Numbers
  1. string str;
  2. char var = 123;
  3. stringstream ss;
  4.  
  5. ss << var;
  6. 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:
Expand|Select|Wrap|Line Numbers
  1. string str;
  2. char var = 123;
  3. stringstream ss;
  4. int temp = var;
  5. ss << temp;
  6. ss >> str;
Apr 17 '13 #2
weaknessforcats
9,208 Expert Mod 8TB
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?
Apr 17 '13 #3
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
Apr 18 '13 #4
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 18 '13 #5
Banfa
9,065 Expert Mod 8TB
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).
Apr 18 '13 #6
Yes it's with visual C++, so the combination of the two I guess
Apr 18 '13 #7
weaknessforcats
9,208 Expert Mod 8TB
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?
Apr 19 '13 #8
Instead of showing actual characters in the label, it just shows the ASCII value of the first character.
Apr 19 '13 #9
weaknessforcats
9,208 Expert Mod 8TB
Like this:

Expand|Select|Wrap|Line Numbers
  1. String str = "label = ";
  2.             char a = 'A';
  3.             char b = 'B';
  4.             str += a;
  5.             str += b;
  6.             Console.WriteLine(str);
?

I didn't see anything in your code snippet about appending the char variales together.
Apr 19 '13 #10
I heard of a string function "atoi" I think. Also, "itoa".
May 4 '13 #11
weaknessforcats
9,208 Expert Mod 8TB
atoi and itoa are archaic C functions. This thread is Managed C++.
May 5 '13 #12
Banfa
9,065 Expert Mod 8TB
Actually itoa isn't even a C standard library function. It is an extension in some platforms is all.
May 7 '13 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Marcio Kleemann | last post by:
I need to force the first letter of each word in a line of text to uppercase. The text comes from a TextBox control in a Web Form. I'm new to ..NET and am having a problem. Since I can't modify...
7
by: MilanB | last post by:
Hello How to convert char to int? Thanks
2
by: claire | last post by:
I have a char that i convert to a string as follows m_tHeader.m_sshortname is defined as char; string result = new string(m_tHeader.m_sshortname); The problem is that any '\0' chars in the...
10
by: sposes | last post by:
Im very much a newbie but perhaps somehone can help me. Ive been searching for a way to convert a std::string to a unsigned char* The situation is I have a function that wants a unsigned char*...
2
by: Joah Senegal | last post by:
Hello I need to convert a chat to a string... but I don't know how to do this. i;ve searched the internet but I've only find some code to convert char * to string or char to string.... but I...
3
by: Kevin Frey | last post by:
I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the new C++/CLI syntax rather than /clr:oldSyntax. Much of our managed code is concerned with interfacing to native C++...
5
by: Zytan | last post by:
I am surprised that a single character string is not auto-created from a single char. It is hard to find information on converting a char into a string, since most people ask how to convert char...
4
by: ACSS | last post by:
can anyone help me how can i convert char to String^ thanks...
3
by: mamul | last post by:
Hi please some one can help me. how to convert char * to string? i have take char *argv from command line and want to pass to a function as string object(string str) i want to first convert argv...
3
by: DaveRook | last post by:
Hi I've had a few error messages with this I don't understand why. It works fine in a Windows Form, but now moving it to a website, it's not working. I don't have the option to count the lines as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.