DorkMonstuh
DorkMonstuh

Reputation: 853

Output float to debug

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.

Upvotes: 0

Views: 3576

Answers (3)

bxs3514
bxs3514

Reputation: 1

Convert the float to wstring with std::to_wstring then use OutputDebugString to print it in the VS output.

Upvotes: 0

S. Pan
S. Pan

Reputation: 629

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());

Upvotes: 0

Eric
Eric

Reputation: 19873

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());

Upvotes: 2

Related Questions