working with numbers in editbox in mfc c++ application

Hello dear experts
I have a editbox in mfc dialog based application.
user must enter a long number in editbox (like 123456789123456789) and the application must make some calculations on it and then show it to user on another editbox.
how can I convert CString to long int and after calculation again convert long int to CStirng to pass to next editbox.
thank you
Saeed VaeziAsked:
Who is Participating?
 
sarabandeConnect With a Mentor Commented:
_wtoll() is exactly spelled like this.

ok. i assume there is a typedef somewhere. you may set the text cursor into the _wtoll and type f12 to see if i am right.

and it not working (I don't know why can you help me why?).

of course. post the compiler errors (if any) from output window and post your source code (the whole function) where the error occurred.

note, the _wtoll (or _wtol_l) isn't so bad as long as you don't care that it returns 0 (or the valid number prefix of the string) if there are some non-digits anywhere between. but the CString::Format actually is an offer to C programmers to not using pure c sprintf function. if you want to program safe c++ code, you should try to avoid the Format function because it is error-prone and not type-safe.

a conversion using stringstream really is the c++ way (beside you were using high-level libraries like boost) and you easily can put the code for it into a little helper class.
// conversion.h
#ifdef conversion_h
#define conversion_h

#include <string>
#include <sstream>

class Conversion
{
public:
     static CString LongLongToCString(long long ll)
     {
           std::ostringstream oss;
           oss << ll;
           return oss.str().c_str(); 
     } 

     static bool CStringToLongLong(const CString & str, long long & ll)
     {
            ll = -1;
            std::istringstream iss(std::string((const char *)str));
            // the conversion must succeed and no rest left in the stream which could not be converted
            if ((iss >> ll) && iss.str().empty()) 
            {
                   return true;
            }
            return false;   
     }
     // add more conversion functions ...
};
#endif

Open in new window



Sara
0
 
Ares KurkluConnect With a Mentor Software EngineerCommented:
you can use std::stoll(str1) to convert str to long and std::to_string(n); to convert long long to string. as seen below:

	std::string str1 = "123456789123456789";

	//convertt string to long long
	long long n = std::stoll(str1);
	std::cout << "str out:" << str1 << " long out:" << n << '\n';

	//convertt long to string
	std::string  str2= std::to_string(n);

	std::cout << " l to str out:" << str2 << '\n';

Open in new window

0
 
sarabandeCommented:
you also can use std::stringstream

#include <sstream>

....

CString strNumber = "123456789123456789";
std::istringstream iss((const char *)strNumber);
long long number = -1;
if (!(iss >> number))
{
     //error string is not numeric
     return;
}
// and back to CString
std::ostringstream oss;
oss << number;
strNumber = oss.str().c_str();

Open in new window

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Saeed VaeziAuthor Commented:
It was very easy
for converting CString to long long int just use _wtoll() function
and for converting long long int to CString again just define a temporary CString like:
CString tempstr;
tempstr.format(L"%lld",your long long int);
0
 
sarabandeConnect With a Mentor Commented:
there is no function _wtoll but it is named _wtol_l.

_wtol_l has no advantage over std::stoll which was posted as a solution by Ares.

both _wtol_l and std::stoll have the disadvantage that they won't throw an error if the string is not a number or the count of digits are exceeding 63-bit integer size (one bit is used as sign bit). std::istringstream::operator>> as shown in my example would provide a proper error return if the string cannot be converted to a 64-bit number.

the CString::Format member function avoids some dangerous buffer handling as it was doene by C function sprintf but is not a type-safe function (since the type of conversion was defined by as text "%s", "%d". if the format doesn't fit to the arguments, you would get a runtime error as the compiler has no chance to handle this properly.

so, both "your" solutions are not recommendable in c++. i would preferably recommend for a deletion rather than to PAQ a non-c++  solution.

Sara
0
 
sarabandeCommented:
if i would have been asked to close the question, I would have recommended an equal split between #a42473406 (assist) and #a42473791 (accept).

but the comment of the Questioner showed that neither solution actually was looked on, let alone tested. by providing an own solution (which is inferior to the given solutions) we have the case that a simple thread which had two suitable answers now had become complex and rarely useful for the knowledgebase.

so, my recommendation is to delete the question.

Sara
0
 
Saeed VaeziAuthor Commented:
I'm new in c++ and mfc. thanks for your answer but I tested your answer on New project in visual studio 2017 mfc project and it not working (I don't know why can you help me why?). But my problem resolved with comment I posted. _wtoll() is exactly spelled like this.
0
 
sarabandeCommented:
There are two valid solutions and the Autor gave feedback. I recommend to close the question with an equal split although we didn't get a final response that the solutions have worked.

Sara
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.