gpt4 book ai didi

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

转载 作者:bug小助手 更新时间:2023-10-28 10:48:10 33 4
gpt4 key购买 nike

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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com