如何在visualc++中将WORD类型转换为字符串

How to convert WORD type to string in Visual C++

本文关键字:类型转换 字符串 WORD 中将 visualc++      更新时间:2023-10-16

谁能解释一下如何将WORD转换为c++中的字符串,请?

  typedef struct _appversion
    {
        WORD            wVersion;  
        CHAR            szDescription[DESCRIPTION_LEN+1];
    } APPVERSION;
    // Some code
    APPVERSION AppVersions;
    // At this point AppVersions structure is initialized properly.
    string wVersion;
    wVersion = AppVersions.wVersion; // Error
// Error    1   error C2668: 'std::to_string' : ambiguous call to overloaded function   
    wVersion = std::to_string((unsigned short)AppVersions.wVersion); 

在visualc++上下文中,WORDunsigned short的类型定义。

所以你可以使用std::to_string来完成这个任务:

 wVersion = std::to_string(AppVersions.wVersion); 

编辑:显然Visual Studio 2010不完全支持c++ 11的特性,使用std::stringstream代替:

std::stringstream stream;
stream <<AppVersions.wVersion;
wVersion  = stream.str();

确保包含<sstream>