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.
  • 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