std::cout 给出了来自 qDebug 的不同输出
std::cout gives different output from qDebug
我正在使用Qt,我有一个unsigned char *bytePointer
,想打印出当前字节的数字值。下面是我的代码,旨在给出我从连接到计算机的计算机接收的连续字节的 int 值和十六进制值:
int byteHex=0;
byteHex = (int)*bytePointer;
qDebug << "n int: " //this is the main issue here.
<< *bytePointer;
std::cout << " (hex: "
<< std::hex
<< byteHex
<< ")n";
}
这给出了完美的结果,我得到了实际的数字,但是这段代码将进入一个API,我不想使用仅限Qt的函数,例如qDebug。所以当我尝试这个时:
int byteHex=0;
byteHex = (int)*bytePointer;
std::cout << "n int: " //I changed qDebug to std::cout
<< *bytePointer;
std::cout << " (hex: "
<< std::hex
<< byteHex
<< ")n";
}
输出确实完美地给出了十六进制值,但是整数值返回符号(如 ☺ 、└、§,列出一些)。
我的问题是:如何让std::cout
给出与qDebug
相同的输出?
编辑:由于某种原因,符号仅在特定的Qt设置下出现。我不知道为什么会发生这种情况,但现在已修复。
正如其他人在评论中指出的那样,您将输出更改为十六进制,但实际上并没有在此处设置它:
std::cout << " (hex: "
<< std::hex
<< byteHex
<< ")n";
之后您需要应用以下内容:
std::cout << std::dec;
标准输出流会将任何字符类型输出为字符,而不是数值。若要输出数值,请转换为非字符整数类型:
std::cout << int(*bytePointer);
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 防止 qDebug() 写入标准输出
- qDebug - 如何以位(二进制格式)输出数据
- 在Qt中:我可以输出到“stdout”吗,就像使用qDebug()输出到“stderr”一样简单
- std::cout 给出了来自 qDebug 的不同输出
- Qt重定向输出从cout到qDebug
- qDebug输出QString UTF-8非ascii符号,如uxxxx
- 如何修复windows上的Qt控制台输出(使用“qInstallMsgHandler”和“qDebug”)