[英]c++ save formatted dword to str variable
I'm new to C++ ,so forgive my ignorance. 我是C ++的新手,请原谅我的无知。
_tprintf(_T("%4.4X-%4.4X"), HIWORD(dwVolumeSerialNumber), LOWORD(dwVolumeSerialNumber))
this prints out for eg : 48AE-2022 . 这打印出来例如:48AE-2022。
i need to store it in a str var to write it to a text file. 我需要将它存储在str var中以将其写入文本文件。 I tried to_string(), but it stores the number 9 which i'm guessing is the number of characters.
我尝试了to_string(),但它存储的数字9,我猜测的是字符数。
TCHAR szBuff[100];
_stprintf_s(szBuff, sizeof(szBuff)/sizeof(TCHAR), _T("%4.4X-%4.4X"), HIWORD(dwVolumeSerialNumber), LOWORD(dwVolumeSerialNumber));
Or you also could do it C++ style: 或者你也可以做C ++风格:
#include <sstream>
#include <string>
std::stringstream ss;
ss<<std::hex<<std::setfill('0')<<std::setw(4)<<HIWORD(dwVolumeSerialNumber)<<"-"<<std::setw(4)<<LOWORD(dwVolumeSerialNumber));
std::string sRes = ss.str();
In that case you should use std::string
or std::wstring
depending on your project unicode settings. 在这种情况下,您应该使用
std::string
或std::wstring
具体取决于您的项目unicode设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.