double to CString and back
-
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.
-
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.
You could use atof, _atof_l, _wtof, _wtof_l[^] functions for your own needs. Good luck and good day!
-
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.
See
char *Char; double Double; Char = " -1234.56"; Double = atof( Char ); CString str; str.Format("%lf",Double);
_**
**_
whitesky
-
See
char *Char; double Double; Char = " -1234.56"; Double = atof( Char ); CString str; str.Format("%lf",Double);
_**
**_
whitesky
-
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 ?
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
-
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
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, orchar* 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 -
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, orchar* 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 Zaci know i know, i fixed with
strcpy()
thanks BTW
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
i know i know, i fixed with
strcpy()
thanks BTW
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
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
-
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
oops, i was meaning
char[30]
, not onlychar
. 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! ]
-
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
Its meCString to Double double atof(const char *string )
If you want error handling use
strtod
and check return value, errno and end-pointer. :(