输出要调试的浮点数
Output float to debug
我想知道是否有替代OutputDebugString,但为浮动代替?因为我希望能够在Visual studio的输出中查看值
首先将float转换为字符串
std::ostringstream ss;
ss << 2.5;
std::string s(ss.str());
然后用这个
打印新生成的字符串OutputDebugString(s.c_str());
可选地,你可以用
跳过中间字符串OutputDebugString(ss.str().c_str());
我把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());
编辑:感谢Quentin的评论,有一个更简单的方法:
std::wstring d2ws(double value) {
std::wostringstream woss;
woss << value;
return woss.str();
}
double theValue=2.5;
OutputDebugString(d2ws(theValue).c_str());
相关文章:
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 使用提升将数据从 PyObject 复制到浮点数 *
- 使用浮点数和双精度数的非常小数字的数学
- 使用英特尔内联函数将打包的 8 位整数乘以浮点数向量
- 如何在 c++ 中将小数点后两位数的浮点数分配给另一个浮点数
- 返回浮点数的小数位数
- txt 文件中浮点数的最大和最小值
- 为什么 std::cout 打印浮点数、双精度和长双精度到相同的小数精度?
- 将浮点数转换为无符号字符数组并打印出来
- 如何将时间字符串 (M:SS) 转换为浮点数
- 如何将浮点数(*)[6]转换为浮点**类型?
- C++将浮点数乘以分数
- 如何将浮点数转换为uint8_t?
- 将字符串转换为浮点数或整数,而无需使用内置函数(如 atoi 或 atof)
- 如何在C++(Arduino)中将浮点数组转换为字节数组
- 在数学上将浮点数四舍五入到 N 位小数
- 我可以控制调试器窗口中显示的浮点数和双精度变量的位数吗?
- 输出要调试的浮点数