I was wondering if there was an alternative to OutputDebugString but for floats instead? As I want to be able to view in the output in Visual studio the values.
Asked
Active
Viewed 3,294 times
0
-
2Well you could wsprintf the float to a buffer and OutputDebugString that? – Rup Apr 28 '13 at 01:08
2 Answers
2
First convert your float to a string
std::ostringstream ss;
ss << 2.5;
std::string s(ss.str());
Then print your newly made string with this
OutputDebugString(s.c_str());
Optionaly you can skip the intermediate string with
OutputDebugString(ss.str().c_str());

Eric
- 19,525
- 19
- 84
- 147
-
1But OutputDebugString() does not accept a [const char *] as an input! It needs to be converted to LPCWSTR first. – mohaghighat Mar 14 '17 at 23:15
0
I combined Eric's answer and Toran Billups' answer from
https://stackoverflow.com/a/27296/7011474
To get:
std::wstring d2ws(double value) {
return s2ws(d2s(value));
}
std::string d2s(double value) {
std::ostringstream oss;
oss << value;
return oss.str();
}
std::wstring s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
double theValue=2.5;
OutputDebugString(d2ws(theValue).c_str());
Edit: Thanks to Quentin's comment there's an easier way:
std::wstring d2ws(double value) {
std::wostringstream woss;
woss << value;
return woss.str();
}
double theValue=2.5;
OutputDebugString(d2ws(theValue).c_str());

S. Pan
- 619
- 7
- 13