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. double to CString and back

double to CString and back

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
12 Posts 8 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.
  • D Desmo16

    Hi all, I need to convert text from an edit control to double in an MFC application. I also need to convert a double into a CString to display it in an edit box. How can i do that ? Thanx in advance, Desmo16.

    S Offline
    S Offline
    see me
    wrote on last edited by
    #3

    You can use For Double to CString CString Format function CString to Double double atof(const char *string ); See msdn once before posting a question Dream bigger... Do bigger...Expect smaller aji

    R 1 Reply Last reply
    0
    • D Desmo16

      Hi all, I need to convert text from an edit control to double in an MFC application. I also need to convert a double into a CString to display it in an edit box. How can i do that ? Thanx in advance, Desmo16.

      stefanmihaimogaS Offline
      stefanmihaimogaS Offline
      stefanmihaimoga
      wrote on last edited by
      #4

      You could use atof, _atof_l, _wtof, _wtof_l[^] functions for your own needs. Good luck and good day!

      1 Reply Last reply
      0
      • D Desmo16

        Hi all, I need to convert text from an edit control to double in an MFC application. I also need to convert a double into a CString to display it in an edit box. How can i do that ? Thanx in advance, Desmo16.

        H Offline
        H Offline
        Hamid Taebi
        wrote on last edited by
        #5

        See

        char *Char; 
        double Double;
           Char = " -1234.56";
           Double = atof( Char );
           CString str;
           str.Format("%lf",Double);
        

        _**


        **_

        whitesky


        D 1 Reply Last reply
        0
        • H Hamid Taebi

          See

          char *Char; 
          double Double;
             Char = " -1234.56";
             Double = atof( Char );
             CString str;
             str.Format("%lf",Double);
          

          _**


          **_

          whitesky


          D Offline
          D Offline
          Desmo16
          wrote on last edited by
          #6

          If i've got a char vector ( example: char name[30]), and i want to convert it into a CString, is there a way to do that ?

          T 1 Reply Last reply
          0
          • D Desmo16

            If i've got a char vector ( example: char name[30]), and i want to convert it into a CString, is there a way to do that ?

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #7

            char name[30] = "hello you";
            CString str = name;
            char name2[30];
            strncpy(name2, str, sizeof(name2));


            TOXCCT >>> GEII power

            [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ] -- modified at 11:12 Wednesday 19th July, 2006

            Z 1 Reply Last reply
            0
            • T toxcct

              char name[30] = "hello you";
              CString str = name;
              char name2[30];
              strncpy(name2, str, sizeof(name2));


              TOXCCT >>> GEII power

              [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ] -- modified at 11:12 Wednesday 19th July, 2006

              Z Offline
              Z Offline
              Zac Howland
              wrote on last edited by
              #8

              toxcct wrote:

              char name[30] = "hello you";
              CString str = name;
              char name2 = str;
              
              char name[30] = "hello you";		// ok
              CString str = name;				// ok
              char name2 = str;				// not good
              

              char name2 = str[0]; is fine, or char* name2 = str.GetBuffer(), but not the other. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

              T 1 Reply Last reply
              0
              • Z Zac Howland

                toxcct wrote:

                char name[30] = "hello you";
                CString str = name;
                char name2 = str;
                
                char name[30] = "hello you";		// ok
                CString str = name;				// ok
                char name2 = str;				// not good
                

                char name2 = str[0]; is fine, or char* name2 = str.GetBuffer(), but not the other. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                T Offline
                T Offline
                toxcct
                wrote on last edited by
                #9

                i know i know, i fixed with strcpy() thanks BTW


                TOXCCT >>> GEII power

                [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                Z 1 Reply Last reply
                0
                • T toxcct

                  i know i know, i fixed with strcpy() thanks BTW


                  TOXCCT >>> GEII power

                  [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                  Z Offline
                  Z Offline
                  Zac Howland
                  wrote on last edited by
                  #10

                  toxcct wrote:

                  char name2; strcpy(name2, str);

                  That still won't compile. You'd have to write it like so:

                  char name2;
                  strcpy(&name2, str);
                  

                  Which is VERY BAD. Since strcpy doesn't check for proper lengths on the source array, it will just start writing to memory at the address (which is the address of a single character) and keep going until its done. That is, it will overwrite at least 1 character in memory that is not allocated for name2 if str is anything but an empty string. I believe what you wanted to show was:

                  char name2[30] = {0};
                  strcpy(name2, str);
                  

                  If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac -- modified at 10:56 Wednesday 19th July, 2006

                  T 1 Reply Last reply
                  0
                  • Z Zac Howland

                    toxcct wrote:

                    char name2; strcpy(name2, str);

                    That still won't compile. You'd have to write it like so:

                    char name2;
                    strcpy(&name2, str);
                    

                    Which is VERY BAD. Since strcpy doesn't check for proper lengths on the source array, it will just start writing to memory at the address (which is the address of a single character) and keep going until its done. That is, it will overwrite at least 1 character in memory that is not allocated for name2 if str is anything but an empty string. I believe what you wanted to show was:

                    char name2[30] = {0};
                    strcpy(name2, str);
                    

                    If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac -- modified at 10:56 Wednesday 19th July, 2006

                    T Offline
                    T Offline
                    toxcct
                    wrote on last edited by
                    #11

                    oops, i was meaning char[30], not only char. and to ensure the length of data copied, strncpy() is the function to use...


                    TOXCCT >>> GEII power

                    [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                    1 Reply Last reply
                    0
                    • S see me

                      You can use For Double to CString CString Format function CString to Double double atof(const char *string ); See msdn once before posting a question Dream bigger... Do bigger...Expect smaller aji

                      R Offline
                      R Offline
                      Roland Pibinger
                      wrote on last edited by
                      #12

                      Its meCString to Double double atof(const char *string )

                      If you want error handling use strtod and check return value, errno and end-pointer. :(

                      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