输出浮动到调试

时间:2013-04-28 01:01:01

标签: c++ c visual-studio

我想知道是否有替代OutputDebugString而不是浮动代替?因为我希望能够在Visual Studio的输出中查看值。

2 个答案:

答案 0 :(得分:1)

首先将您的浮动转换为字符串

std::ostringstream ss;
ss << 2.5;
std::string s(ss.str());

然后使用此

打印新制作的字符串
OutputDebugString(s.c_str());

Optionaly你可以用

跳过中间字符串
OutputDebugString(ss.str().c_str());

答案 1 :(得分:0)

我结合了Eric的回答和Toran Billups的回答,来自 https://stackoverflow.com/a/27296/7011474 获取:

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

编辑:感谢昆汀的评论,有一个更简单的方法:

std::wstring d2ws(double value) {
    std::wostringstream woss;
    woss << value;
    return woss.str();
}

double theValue=2.5;
OutputDebugString(d2ws(theValue).c_str());