C ++ store 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 outputs, for example, on 48AE-2022.

I need to store it in str var in order to write it to a text file. I tried to_string (), but it keeps the number 9, which I am guessing is the number of characters.

+3


source to share


1 answer


TCHAR szBuff[100];
_stprintf_s(szBuff, sizeof(szBuff)/sizeof(TCHAR), _T("%4.4X-%4.4X"), HIWORD(dwVolumeSerialNumber), LOWORD(dwVolumeSerialNumber));

      

Or you can also do it in 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 this case, you must use std::string

or std::wstring

depending on the project yuecode settings.

+2


source







All Articles