ostream showbase不显示"0x".零值 [英] ostream showbase does not show "0x" for zero value

查看:154
本文介绍了ostream showbase不显示"0x".零值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

PSPS: (a Pre-scripted Post-script)
It has just come to mind that a more prescient question would have included the notion of: Is this non-display of "0x"(showbase) for zero-value integers a standard behaviour, or is it just a quirk of my MinGW implementation?

这一切都始于一个愉快的星期天早上...我想以其十六进制表示形式并以一种一致,格式化的方式来处理一些句柄.
我想要一个前导0x 和一个固定宽度,但是事实证明,使用预期的流操纵器很难做到这一点.
我发现这样做的唯一方法是将Handles强制转换为无符号的long. 这似乎有点不合理,我希望我不是唯一一个想要这个的人.
我在标准的六角形操纵器中是否缺少某些东西?是否是因为void *(HANDLE)类型只是在ostream的常规十六进制处理之外定义的?

It all began on a pleasant Sunday morning... I want to dump some Handles in their hex representation, and in a consistant, formatted way.
I want a leading 0x and a fixed width, but this is proving to be elusive using the expected stream manipulators.
The only way I've found to do this is to cast the Handles to an unsigned long. This seems a bit unreasonable, and I would expect that I'm not the only person to have ever wanted this..
Am I missing something in the standard hex manipulators? Is it because type void* (HANDLE) is simply defined outside of the normal hex-handling of ostream?

总结:我不想将HANDLE强制转换为非强制性内容.
而且我不想硬编码"0x"前缀.有没有办法使用标准操纵器来做到这一点?还是我需要重载ostream对HANDLE的处理? (但这可能会让我超负荷!)

In summary: I don't want to have to cast HANDLE to something which it isn't.
and I don't want to hard code a "0x" prefix. Is there a way to do it using standard manipulators? or would I need to overload ostream's handling of HANDLE? (but that might overload me!)

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

Here is my test code (and its output).
I've used '.' as the fill char, for clarity, (I will actually be using '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 -直接从那里复制/粘贴.

I found this on https://bugzilla.redhat.com/show_bug.cgi?id=166735 - this is copy/paste straight from there.

对我来说听起来不像是个虫子. ISO C ++ 98,22.2.2.2.2/10说std :: showbase意味着在#printf转换之前 限定词. 22.2.2.2.2/7说std :: hex表示printf转换说明符为 %X. 因此,恕我直言,该行为要求与printf(%#x",0)相同; 但是 http://www.opengroup.org/onlinepubs/009695399/functions/fprintf. html 说: 对于x或X转换说明符,非零结果应为0x(或0X) 加上前缀." ISO C99,7.19.6.1(6)中也是如此: 对于x(或X)转换,非零结果的前缀为0x(或0X)."

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.抱歉.

So it sounds like the C++98 standard (by saying 'make it like C's printf("%#x", 0)') requires this goofy behavior you're seeing. The only way to get what you want would be to get rid of the std::showbase and output 0x explicitly. Sorry.

这篇关于ostream showbase不显示"0x".零值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆