【问题标题】:CString values gets truncated during 64bit castingCString 值在 64 位转换期间被截断
【发布时间】:2017-02-16 09:02:45
【问题描述】:

当我从 Ulong64 转换为 cstring 值被截断为 64 位时,任何人都可以帮助我吗?

HMONITOR hmonitor64;  // Hmonitor decl  
hmonitor64 = (HMONITOR)0x0000000300290eaf;// initialize to big value
ULONG64 lmonitor64;
CString strMonitor64;
lmonitor64 = (ULONG64)hmonitor64; // typecasted to long 
strMonitor64.Format(_T("%lu"), lmonitor64);  // value gets truncated in cstring

【问题讨论】:

  • 请格式化您的源代码。 (使用编辑器的工具按钮“{}”。)
  • 如果没有任何研究,我猜你必须使用"%llu"long 表示 Windows/VC 中的 32 位,即使在 64 位平台上也是如此。所以,你必须使用long long unsigned 或(就像你一样)ULONG64
  • 截断也发生在 32 位代码中。您只是没有注意到,因为HMONITOR 在 32 位代码中是 32 位宽。

标签: c++ mfc


【解决方案1】:

格式化ULONG64的正确方法如下:

HMONITOR hmonitor64;  // Hmonitor decl  
hmonitor64 = (HMONITOR)0x0000000300290eaf;// initialize to big value
ULONG64 lmonitor64;
CString strMonitor64;
lmonitor64 = (ULONG64)hmonitor64; // typecasted to long 
strMonitor64.Format(_T("%I64u"), lmonitor64);  // value gets truncated in cstring

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    相关资源
    最近更新 更多