How to write INT64 to CString

Markus Freitag 3,791 Reputation points
2024-01-14T17:02:36.2966667+00:00

Hello,

How can I correctly convert a large number of integers into a CString?

How can I see what auto is? long, int, double, object...?

long difference_ms = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - std::chrono::system_clock::from_time_t(std::mktime(&dbTime))).count();
auto difference_seconds = std::chrono::duration_cast<std::chrono::seconds>(currentTime - std::chrono::system_clock::from_time_t(std::mktime(&dbTime))).count();
auto difference_seconds3 = std::chrono::duration_cast<std::chrono::seconds>(currentTime - std::chrono::system_clock::from_time_t(std::mktime(&dbTime))).count();

// %lld and %I64d both work equally as well.--> No....not work

//sHelp.Format("Time difference in seconds= '%d', in milliseconds='%lld'", difference_seconds, difference_ms);
sHelp.Format("Time difference in seconds= '%d'", difference_seconds);

CString sHelp;
%lld and %I64d both work equally as well.
strCode.Format(_T("Code = %lld, Result = %I64d \n"),lCode,lResult);

enter image description here

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,845 questions
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 46,851 Reputation points
    2024-01-14T18:19:34.0266667+00:00

    Another thing to consider -

    auto t1 = std::chrono::system_clock::now();
    std::this_thread::sleep_for(std::chrono::seconds(5));
    auto t2 = std::chrono::system_clock::now();
    auto d = t2 - t1;
    auto s = std::chrono::duration_cast<std::chrono::seconds>(d);
    auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(d);
    std::cout << "Seconds " << s.count() << ", milliseconds " << ms.count() << std::endl;
    CStringW strw_s(std::to_wstring(s.count()).c_str());
    CStringW strw_ms(std::to_wstring(ms.count()).c_str());
    CStringA stra_s(std::to_string(s.count()).c_str());
    CStringA stra_ms(std::to_string(ms.count()).c_str());
    printf_s("Seconds(ansi): %s, milliseconds(ansi): %s\n", (LPCSTR) stra_s, (LPCSTR) stra_ms);
    wprintf_s(L"Seconds(UNICODE): %s, milliseconds(UNICODE): %s\n", (LPCWSTR)strw_s, (LPCWSTR)strw_ms);
    

2 additional answers

Sort by: Most helpful
  1. Barry Schwarz 3,331 Reputation points
    2024-01-14T18:03:37.6733333+00:00

    You show us the type of difference_ms but the variable you are trying to look at is difference_seconds. It seems unlikely that it is an int. If you want to print it with %d, then cast it to int. Based on yur naming convention, I expect lCode and lResut to have type long int. If you want to print them with %lld (instead of %ld), cast each to long long int. If you let auto decide the type, then you have to take into account that the type may change as your code evolves. The obvious solution for type/format mismatches is to cast the value to match the format.

    0 comments No comments

  2. WayneAKing 4,926 Reputation points
    2024-01-14T18:32:39.0766667+00:00

    >How can I see what auto is? long, int, double, object...?

    In the debugger, when you stop on a breakpoint after an auto statement, look at the type shown in the Locals window.

    • Wayne auto type

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.