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

问题描述 投票:0回答:2

谁能解释一下如何用 C++ 将 WORD 转换为字符串吗?

  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); 
c++ visual-c++
2个回答
2
投票
Visual C++ 上下文中的

a

WORD
unsigned 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>


0
投票

嗯,你为什么把事情搞得这么复杂?

std::string((char*)&AppVersions.wVersion, sizeof AppVersions.wVersion /*or just 2*/)
© www.soinside.com 2019 - 2024. All rights reserved.