简体   繁体   中英

c++ save formatted dword to str variable

I'm new to C++ ,so forgive my ignorance.

_tprintf(_T("%4.4X-%4.4X"), HIWORD(dwVolumeSerialNumber), LOWORD(dwVolumeSerialNumber))

this prints out for eg : 48AE-2022 .

i need to store it in a str var to write it to a text file. I tried to_string(), but it stores the number 9 which i'm guessing is the number of characters.

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:

#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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM