CString Basics
-
CString has a GetBuffer function which gives you a char*. Call ReleaseBuffer when you're done with it. You cannot use += on a class object, unless operator + is defined. The [] operator works in a CString though. Christian I am completely intolerant of stupidity. Stupidity is, of course, anything that doesn't conform to my way of thinking. - Jamie Hale - 29/05/2002 Half the reason people switch away from VB is to find out what actually goes on.. and then like me they find out that they weren't quite as good as they thought - they've been nannied. - Alex, 13 June 2002
-
Thats odd, += works fine... it doesnt appear to be the problem, I input "hi" it returns "hi". I want it to return "104105" (h-104 i-105)
Then you should use Left() and Mid() It's odd to me that += works, but I admit I've never tried it. Christian I am completely intolerant of stupidity. Stupidity is, of course, anything that doesn't conform to my way of thinking. - Jamie Hale - 29/05/2002 Half the reason people switch away from VB is to find out what actually goes on.. and then like me they find out that they weren't quite as good as they thought - they've been nannied. - Alex, 13 June 2002
-
Hmm... Sorry for posting so much code, but I need to give you all an idea of what im doing :) CString toASCII(CString input) { int i = 0; int int_buffer; int int_output; char byte_buffer; CString output; while(i < input.GetLength()) { byte_buffer = input.GetAt(i); int_buffer = byte_buffer; output += int_buffer; i++; } return output; } ----- What I get out is what I put in. What im trying to do is get the ASCII number value out. I know that my problem is probably on this line: output += int_buffer; The CString is reconizing my int as a byte, instead of a number. How can i do int a = 132; CString str; str = a; and have str contain "123"? At least i think thats where my problem is, maybe it isn't. Thanks for all the help.
try this: int x = 123; CString s; s.Format("%d", x); // now s = "123" -- this a string representation of the integer.
-
Then you should use Left() and Mid() It's odd to me that += works, but I admit I've never tried it. Christian I am completely intolerant of stupidity. Stupidity is, of course, anything that doesn't conform to my way of thinking. - Jamie Hale - 29/05/2002 Half the reason people switch away from VB is to find out what actually goes on.. and then like me they find out that they weren't quite as good as they thought - they've been nannied. - Alex, 13 June 2002
Yes, += works just fine. I use it quite often.
-
Yes, += works just fine. I use it quite often.
Doh. It concatenates strings. My first reading of that code made me think he was trying to do pointer arithmetic with it. Thanks. Christian I am completely intolerant of stupidity. Stupidity is, of course, anything that doesn't conform to my way of thinking. - Jamie Hale - 29/05/2002 Half the reason people switch away from VB is to find out what actually goes on.. and then like me they find out that they weren't quite as good as they thought - they've been nannied. - Alex, 13 June 2002
-
try this: int x = 123; CString s; s.Format("%d", x); // now s = "123" -- this a string representation of the integer.
One more question (thanks, you have all been very helpful so far). How can i do: int x; CString str = "5"; x = str; and have x = 5? Is there a function that can be used to convert to a int from a CString? I assume its probably similar to the CString.Format()? Thanks!
-
One more question (thanks, you have all been very helpful so far). How can i do: int x; CString str = "5"; x = str; and have x = 5? Is there a function that can be used to convert to a int from a CString? I assume its probably similar to the CString.Format()? Thanks!
int x = atoi(str.GetBuffer(0));
-
Hmm... Sorry for posting so much code, but I need to give you all an idea of what im doing :) CString toASCII(CString input) { int i = 0; int int_buffer; int int_output; char byte_buffer; CString output; while(i < input.GetLength()) { byte_buffer = input.GetAt(i); int_buffer = byte_buffer; output += int_buffer; i++; } return output; } ----- What I get out is what I put in. What im trying to do is get the ASCII number value out. I know that my problem is probably on this line: output += int_buffer; The CString is reconizing my int as a byte, instead of a number. How can i do int a = 132; CString str; str = a; and have str contain "123"? At least i think thats where my problem is, maybe it isn't. Thanks for all the help.
Read the VC forum FAQ which has an entry on converting numbers to their string representations. --Mike-- Just released - RightClick-Encrypt - Adds fast & easy file encryption to Explorer My really out-of-date homepage Sonork-100.19012 Acid_Helm
-
One more question (thanks, you have all been very helpful so far). How can i do: int x; CString str = "5"; x = str; and have x = 5? Is there a function that can be used to convert to a int from a CString? I assume its probably similar to the CString.Format()? Thanks!
Try this.
int x;
CString str ="5";x = _ttoi(str); //Converts a string to a integer.
I think there is another function like that for Unicode also but off the top of my head I can't think of it. Cheers :)
-
Hello, I'll admit i'm new to C++ and visual studio. Most of my programming experience comes from c# and php. That said, I know basic C++, C, that kind of stuff, and am trying to learn to use visual C++. So what I set out to do is make a simple dialog application that would take a input from a Edit control, change it into it's ASCII value, and replace the message with its ASCII value in the edit box. Here is how I planed on doing it: Take input-- UpdateData(FALSE); //get the data into the variable Create byte array to store the chars in. (unsure) Cycle through the bytes (for loop) Extract the ASCII values and put them into a int array (no idea, can I cast in visual C++, ie: (int) byte) Somehow cycle through the int array and put them into a Cstring (unsure) then UpdateData(TRUE) and put it back up... So my questions: 1) Am I going about this in an ok way 2) Could I have a example of how you would put a CString into a byte array (I keep getting annoying errors about various stuff). 3) How can i get the ASCII value of a byte and put it into a int variable? 4) How can I cycle through a int array and put the values into a Cstring Another annoying thing to me is that when i try and do CString str; str = "hello" int lengh; lengh = str.GetLengh(); I get a error that 'GetLengh' is not a member of 'CString', though according to MSDN it is. Thanks for the help!