c++ - ostream showbase 不显示 "0x"为零值

PSPS:(预先编写好的后置脚本)
刚刚想到一个更有先见之明的问题将包括以下概念:对于零值整数,这种不显示“0x”(showbase)是一种标准行为,还是只是我的 MinGW 实现的一个怪癖?

这一切都始于一个愉快的星期天早上...我想以十六进制表示形式转储一些句柄,并以一致的格式化方式。
我想要一个领先的 0x 和一个固定宽度,但事实证明,使用预期的流操纵器这是难以捉摸的。
我发现这样做的唯一方法是将句柄转换为无符号长整数。 这似乎有点不合理,我希望我不是唯一一个想要这个的人..
我在标准十六进制操纵器中遗漏了什么?是因为 void* (HANDLE) 类型只是在 ostream 的正常十六进制处理之外定义的吗?

总结:我不想将 HANDLE 强制转换为它不是的东西。
而且我不想硬编码“0x”前缀。有没有办法使用标准机械手来做到这一点?还是我需要重载 ostream 对 HANDLE 的处理? (但这可能会让我重载!)

这是我的测试代码(及其输出)。
我用过'。作为填充字符,为清楚起见,(我实际上将使用'0')

HANDLE h; 
ULONG ul; 
int iH = sizeof(h); // how many bytes to this void* type.
int iW = iH*2;      // the max number of hex digits (width).
int iW2= iW+2;      // the max number of hex digits (+ 2 for showbase "0x").
int iX = 4;         // the number of bits per hex digit.
int ib = iH*8;      // the max number bits in HANDLE (exponent).
int i;
std::cout<<std::setfill('.'); // I actually want '0'; 
                              //   the dot is for display clarity
for( i=0; i<=ib; i+=iX )
{ ul = (pow(2,i)-1);
  h  = (HANDLE)ul;
  std::cout
  <<"//  ul "        <<std::setw(iW2)<<std::hex <<std::showbase  <<std::internal <<ul
  <<"     h "        <<std::setw(iW2) /* hex,showbase,internal have no effect */ <<h
  <<"      I want 0x"<<std::setw(iW) <<std::hex <<std::noshowbase<<std::right    <<(ULONG)h
  <<std::endl;
}

//  ul .........0     h .........0      I want 0x.......0
//  ul 0x.......f     h .......0xf      I want 0x.......f
//  ul 0x......ff     h ......0xff      I want 0x......ff
//  ul 0x.....fff     h .....0xfff      I want 0x.....fff
//  ul 0x....ffff     h ....0xffff      I want 0x....ffff
//  ul 0x...fffff     h ...0xfffff      I want 0x...fffff
//  ul 0x..ffffff     h ..0xffffff      I want 0x..ffffff
//  ul 0x.fffffff     h .0xfffffff      I want 0x.fffffff
//  ul 0xffffffff     h 0xffffffff      I want 0xffffffff

最佳答案

我在 https://bugzilla.redhat.com/show_bug.cgi?id=166735 上找到了这个- 这是直接从那里复制/粘贴。

Doesn't sound like a bug to me. ISO C++98, 22.2.2.2.2/10 says std::showbase means prepending # printf conversion qualifier. 22.2.2.2.2/7 says std::hex means the printf conversion specifier is %x. So the behaviour is IMHO required to be the same as printf ("%#x", 0); But http://www.opengroup.org/onlinepubs/009695399/functions/fprintf.html says: "For x or X conversion specifiers, a non-zero result shall have 0x (or 0X) prefixed to it." The same is in ISO C99, 7.19.6.1(6): "For x (or X) conversion, a nonzero result has 0x (or 0X) prefixed to it."

所以这听起来像 C++98 标准(通过说'让它像 C 的 printf("%#x", 0)')需要你看到的这种愚蠢的行为。获得所需内容的唯一方法是摆脱 std::showbase 并显式输出 0x 。对不起。

关于c++ - ostream showbase 不显示 "0x"为零值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3273654/

相关文章:

floating-point - 如何格式化具有特定精度和前置零的 f32?

python - 设置 ipython 的默认科学记数法阈值

bash - UNIX 统计时间格式

android - HTML.fromHtml 换行符消失

formatting - 抑制 Fortran 95 写入语句中的换行符

python - 将任意文本格式化为漂亮的 HTML 的 Python 模块的名称是什么?

vim - 在 Vim 中轻松将函数参数重新格式化为多行

asp.net-mvc - 电话号码或社会保险号的 DisplayFormat.DataFormat

java - 是否可以使用 String.format 作为条件小数点?

java - 如何在 IntelliJ 中停止流式 API 的扁平化