Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CString Basics

CString Basics

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++helpquestionphp
16 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    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!

    N A A 3 Replies Last reply
    0
    • L Lost User

      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!

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #2

      Greg_ wrote: I get a error that 'GetLengh' is not a member of 'CString', though according to MSDN it is. That must be a chinese translated MSDN then :-) It's GetLength <- notice the 't' you missed Nish


      Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

      L 1 Reply Last reply
      0
      • N Nish Nishant

        Greg_ wrote: I get a error that 'GetLengh' is not a member of 'CString', though according to MSDN it is. That must be a chinese translated MSDN then :-) It's GetLength <- notice the 't' you missed Nish


        Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        I mis-typed it into the web-form, I assure you I spelled it correctly in the C++ program :)

        1 Reply Last reply
        0
        • L Lost User

          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!

          A Offline
          A Offline
          Ancient Dragon
          wrote on last edited by
          #4

          First, I think you, like may others, are confused about how computers actually store characters. In the English language (non-Englich languages may be store differently), each character (both printable and non-printable) is represented by a numeric value between 0 and 255. (Actually, at the machine level, they are represented by their binary values, but I won't go there.) You cannot put an "A" into either a CString or a char buffer. Instead, the "A" is always convertd to its numeric value. Charts are easily available that show what these numeric values are. The Basic languages that I've used (although very seldon) have a function to convert "A" to its numeric equivalent. This is not necessary in C/C++, because they are already represented that way. So, if you have a CString that contains "Hello", then what that CString really contains is six bytes of numeric values (the 6th byte contains the null terminator 0 ). How do you convert a "byte" to an "int" ? Simple: char c = 'A'; unsigned char = 'A'; int x = 'A'; int x1 = 64; // numeric value of 'A' int x2 = c; // converts a char to an int. CString s = "Hello"; char c1 = s[1]; // extract the letter 'e' from CString char buffer[126]; strcpy(buffer,s.GetBuffer(0)); // copy the contents of CString into a char buffer I don't know why you would want to put a string array into an int array -- they can easily be represented in a char array (see above). But, if you did do that, you can put them into a CString like this: int array[] = {'H','e','l','l','o',0 }; CString string; for (int i = 0; array[i] != 0; i++) string += (char)array[i]; // notice you need to type cast from int to char

          L 1 Reply Last reply
          0
          • A Ancient Dragon

            First, I think you, like may others, are confused about how computers actually store characters. In the English language (non-Englich languages may be store differently), each character (both printable and non-printable) is represented by a numeric value between 0 and 255. (Actually, at the machine level, they are represented by their binary values, but I won't go there.) You cannot put an "A" into either a CString or a char buffer. Instead, the "A" is always convertd to its numeric value. Charts are easily available that show what these numeric values are. The Basic languages that I've used (although very seldon) have a function to convert "A" to its numeric equivalent. This is not necessary in C/C++, because they are already represented that way. So, if you have a CString that contains "Hello", then what that CString really contains is six bytes of numeric values (the 6th byte contains the null terminator 0 ). How do you convert a "byte" to an "int" ? Simple: char c = 'A'; unsigned char = 'A'; int x = 'A'; int x1 = 64; // numeric value of 'A' int x2 = c; // converts a char to an int. CString s = "Hello"; char c1 = s[1]; // extract the letter 'e' from CString char buffer[126]; strcpy(buffer,s.GetBuffer(0)); // copy the contents of CString into a char buffer I don't know why you would want to put a string array into an int array -- they can easily be represented in a char array (see above). But, if you did do that, you can put them into a CString like this: int array[] = {'H','e','l','l','o',0 }; CString string; for (int i = 0; array[i] != 0; i++) string += (char)array[i]; // notice you need to type cast from int to char

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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.

            C A M 3 Replies Last reply
            0
            • L Lost User

              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.

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              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

              L 1 Reply Last reply
              0
              • C Christian Graus

                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

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                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)

                C 1 Reply Last reply
                0
                • L Lost User

                  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)

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  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

                  A 1 Reply Last reply
                  0
                  • L Lost User

                    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.

                    A Offline
                    A Offline
                    Ancient Dragon
                    wrote on last edited by
                    #9

                    try this: int x = 123; CString s; s.Format("%d", x); // now s = "123" -- this a string representation of the integer.

                    L 1 Reply Last reply
                    0
                    • C Christian Graus

                      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

                      A Offline
                      A Offline
                      Ancient Dragon
                      wrote on last edited by
                      #10

                      Yes, += works just fine. I use it quite often.

                      C 1 Reply Last reply
                      0
                      • A Ancient Dragon

                        Yes, += works just fine. I use it quite often.

                        C Offline
                        C Offline
                        Christian Graus
                        wrote on last edited by
                        #11

                        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

                        1 Reply Last reply
                        0
                        • A Ancient Dragon

                          try this: int x = 123; CString s; s.Format("%d", x); // now s = "123" -- this a string representation of the integer.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          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!

                          A J 2 Replies Last reply
                          0
                          • L Lost User

                            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!

                            A Offline
                            A Offline
                            Ancient Dragon
                            wrote on last edited by
                            #13

                            int x = atoi(str.GetBuffer(0));

                            1 Reply Last reply
                            0
                            • L Lost User

                              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.

                              M Offline
                              M Offline
                              Michael Dunn
                              wrote on last edited by
                              #14

                              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

                              1 Reply Last reply
                              0
                              • L Lost User

                                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!

                                J Offline
                                J Offline
                                Jay Beckert
                                wrote on last edited by
                                #15

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

                                1 Reply Last reply
                                0
                                • L Lost User

                                  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!

                                  A Offline
                                  A Offline
                                  aldeba
                                  wrote on last edited by
                                  #16

                                  Out of topic. Interesting to see someone from C# coming to C++. Job requirements make you do this, or our of shear interest?

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups