0x%08lx 格式说明符在 printf 语句到 cout
0x%08lx format specifier in printf statement to cout
我有一个printf语句:
printf("Buffer A Data: 0x%08lx, Buffer B Data: 0x%08lxnn", (ul) bufa, (ul) bufb);
如何使用std::cout
编写此语句?缓冲区变量包含巨大的地址。
您可以使用std::setw
、std::setfill
和std::hex
修饰符:
std::cout << "0x" << std::hex << std::setw(8) << std::setfill('0') << bufa;
笔记:
- 打印内容后
std::setw
重置 - 正如Tarang Jain所指出的,
std::showbase
会将基础标记放在填充物内(即000xDEAD
而不是0x0000DEAD
(,所以我编辑了这个答案来反映这一点。 - 您可能需要
#include <iomanip>
将此 - iomanips 包装在
std::ios_base::fmtflags f{cout.flags()};
以保存标志和cout.flags(f);
将它们重置为以前的值之间可能是个好主意,有关更多信息,请参阅此问题 - 这个答案提供了一种使用 iomanips 显示事物的方法,另请参阅杰里·科芬的答案,只需打印内存地址即可
一个更完整的示例,使用对象利用 RAII 清除标志
#include <iomanip>
#include <ostream>
struct addressPrinter
{
addressPrinter(std::ostream& out)
: out{out}, flags{out.flags()}
{
}
~addressPrinter()
{
out.flags(flags);
}
void print(unsigned long address)
{
out << "0x" << std::hex << std::setw(8) << std::setfill('0') << address;
}
std::ostream& out;
std::ios_base::fmtflags flags;
};
void foo()
{
// ...
addressPrinter printer{std::cout};
printer.print(bufa);
printer.print(bufb);
// ...
}
你在 C 中所做的(显然(是将指针转换为无符号整数类型,然后以十六进制打印出该值。
但是,C++专门为指针提供了转换:
std::cout << "Buffer A Data: " << static_cast<void *>(bufa)
<< ", Buffer B Data: " << static_cast<void *>(bufb) << "nn";
在大多数典型的当前实现中,这可能会产生几乎相同的结果(即,十六进制的无符号数字,可能为零填充以产生目标系统上寻址的宽度(。
由于这是更抽象的,它可以适应目标系统的约定。一个目标系统通常可能会省略前导0x
。另一个通常可能使用八进制而不是十六进制。而且,当然,它通常不会在使用 32 位寻址的系统上打印出 64 位地址。相反,这可以在目标系统上生成接受的约定,而不是显式指定一个恰好与您习惯的系统匹配的约定。
相关文章:
- 在 COUT 语句中使用 COUT 调用函数
- C++程序在一定行后未正确编译[cout语句]
- 这种递归是如何到达 COUT 语句的?
- 0x%08lx 格式说明符在 printf 语句到 cout
- 我收到一个断言错误,但是当我编写 cout 语句时,它会消失
- 一个语句中没有多个 cout 的错误/警告
- 为什么 cout 语句在传递指向函数的指针时没有执行?
- 两个 cin / cout 语句合并为一个
- 在 cout 语句中首先运行什么?(C++17)
- if 语句仅在前面有调试 cout 行(C 中的多线程)时才通过
- 使用自定义宏来简化 cin 和 cout 语句等内容被认为是更好还是更差
- 单个cout中的多个语句产生不同的结果
- 尽管我的功能和cout语句的放置错误,但我的程序为什么会起作用
- 如何在COUT语句中使用移动运算符
- 如果语句在第一次尝试后没有给出 cout
- C :Bool方法在COUT语句之后返回意外数字
- cout 输出乱码消息行,而不是实际用户的 cout 语句
- 添加 cout 语句会更正输出
- 如何仅使用循环/控制语句和两个cout语句创建一个倒三角:一个输出“#”和一个输出' n'
- 为什么语句"cout << '\\\\';"没有失败?