__int64 to CString returns wrong values - C++ MFC

standard conversions c++
conversion of signed to unsigned integer in c
c++11 pointer cast
c unsigned to signed
type conversion in c++
type casting in c++ pdf
typecast in c++
c signed/unsigned conversion

I want to convert a __int64 variable into a CString. The code is exactly this

__int64 i64TotalGB;
CString totalSpace;
i64TotalGB = 150;
printf("disk space: %I64d GB\n", i64TotalGB);
totalSpace.Format(_T("%I64d", i64TotalGB));
printf("totalSpace contains: %s", totalSpace);

the first printf prints

"disk space: 150GB"

which it's correct, but the second printf prints randomly high numbers like "totalSpace contains: 298070026817519929"

I also tried to use a INT64 variable instead of a __int64 variable, but the result is the same. What can be the cause of this?

Here:

totalSpace.Format(_T("%I64d", i64TotalGB));

you're passing i64TotalGB as an argument to the _T() macro instead of passing it as the second argument to Format().

Try this:

totalSpace.Format(_T("%I64d"), i64TotalGB);

Having said that, thanks to MS's mess (ha) around character encodings, using _T here is not the right thing, as CString is composed of a TCHAR and not _TCHAR. So taking that into account, might as well use TEXT() instead of T(), as it is dependent on UNICODE and not _UNICODE:

totalSpace.Format(TEXT("%I64d"), i64TotalGB);

In addition, this line is wrong as it tries to pass an ATL CString as a char* (a.k.a. C-style string):

printf("totalSpace contains: %s", totalSpace);

For which the compiler gives this warning:

warning C4477: 'printf' : format string '%s' requires an argument of type 'char *', but variadic argument 1 has type 'ATL::CString'

While the structure of CString is practically compatible with passing it like you have, this is still formally undefined behavior. Use CString::GetString() to safeguard against it:

printf("totalSpace contains: %ls", totalSpace.GetString());

Note the %ls as under my configuration totalSpace.GetString() returned a const wchar_t*. However, as "printf does not currently support output into a UNICODE stream.", the correct version for this line, that will support characters outside your current code page, is a call to wprintf() in the following manner:

wprintf("totalSpace contains: %s", totalSpace.GetString());

Having said ALL that, here's a general advice, regardless of the direct problem behind the question. The far better practice nowadays is slightly different altogether, and I quote from the respectable answer by @IInspectable, saying that "generic-text mappings were relevant 2 decades ago".

What's the alternative? In the absence of good enough reason, try sticking explicitly to CStringW (A Unicode character type string with CRT support). Prefer the L character literal over the archaic data/text mappings that depend on whether the constant _UNICODE or _MBCS has been defined in your program. Conversely, the better practice would be using the wide-character versions of all API and language library calls, such as wprintf() instead of printf().

__int64 to CString returns wrong values - C++ MFC, I want to convert a __int64 variable into a CString. The code is exactly this __​int64 i64TotalGB; CString totalSpace; i64TotalGB = 150; printf("disk space: %I64d​  I'm currently working on a MFC program that specifically has to work with UTF-8. At some point, I have to write UTF-8 data into a file; to do that, I'm using CFiles and CStrings. When I get to write utf-8 (russian characters, to be more precise) data into a file, the output looks like Ðàñïå÷

Type conversions and type safety, In a widening conversion, a value in a smaller variable is assigned to a larger Any signed or unsigned integral type except long long or __int64, double data loss so that it does not cause your program to produce incorrect results. The C-​style cast operator is identical to the call operator () and is  I am reading/writing Ini file in MFC.The class which i am using is CIniFile written in C++.Now when i GetValue() from .inifile,it returns value in std::string type.But i want to store that value in CString type variable.How can we convert it .

try this

totalSpace.Format(_T("%I64d"), i64TotalGB);

C# for MFC programmers Quick Equivalents Map, Formatting specifications in C# are much more flexible than in traditional C. For CWnd::ShowWindow(SW_HIDE); .Visible = false;. CString.GetLength(); I. _I64_MAX, Int64.MaxValue. _I64_MIN, Int64.MinValue. int, int. __int64, long Location member returns a copy of the Point value, so changing the values in this will  Convert string to int _atoi64, _atoi64_l: Convert string to __int64 or long long: atol, _atol_l: Convert string to long: c16rtomb, c32rtomb: Convert UTF-16 or UTF-32 character to equivalent multibyte character: _ecvt, _ecvt_s: Convert double to string of specified length: _fcvt, _fcvt_s: Convert double to string with specified number of digits

Questions for tag int64, TypeError when initializing data frame with Int64 data type __int64 to CString returns wrong values - C++ MFC A int64_t value overflows in a structure in C. return strReturn; }. As you can see, A CString object is created local to the function, manipulated, and then returned by value. With most objects this would be completely illegal because the object would be distroyed at the end of the function, and you'd be returning basically a block of freed memory to the caller.

PLIN-API_ExampleDlg.cpp, Checksum); return result; } CString CMessageStatus::getErrorString() CString result; char buffer[65]; unsigned __int64 time; memset(buffer,'\0',64); time = m_Msg. GetExtendedStyle()|LVS_EX_FULLROWSELECT); // Setup MFC PropertyGrid COleVariant value; CString strTemp; CMFCPropertyGridProperty* pProp;  Output buffer length. C++ integers are typically 32-bits, with values as high as billions; the range is: 0 to 4,294,967,295 (unsigned) -2,147,483,648 to 2,147,483,647 (signed)Thus, the maximum length of the resulting string is 12 characters (including NULL terminator and not including commas or other formatting).

[PDF] C++ Language Reference, Object Lifetime And Resource Management (Modern C++). Objects Own false true char, wchar_t, char16_t, char32_t. __int8, __int16, __int32, __int64 Every variable, function argument, and function return value must (If you are using MFC or ATL, the CString class is also available, but is not part of the C++ standard.)  Javascript doesn't have a 64-bit integer type. Numbers will be represented by the Number type[^], which is a 64-bit double precision floating-point type. The largest integer that the Number type can store is 9007199254740992; anything larger than that will lose precision.

Comments
  • General advice: Stop pretending, that you were targeting Win9x. You aren't. Use CStringW in place of CString, replace _T and similar macros with an L prefix, and live a happy life ever after. In other words: CStringW totalSpace; totalSpace.Format(L"%I64d", i64TotalGB); wprintf(L"totalSpace contains: %s", totalSpace.GetString());.
  • @IInspectable I am now seeing it in the docs: "printf does not currently support output into a UNICODE stream." -- made an edit to reflect. quite a detail but important enough to note IMHO