本文介绍了ostream showbase不存在为零值,内部不工作的HANDLE?我必须Ige吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PSPS:(一个预编写的后脚本)

刚刚想到一个更有先见的问题会包括以下概念:显示0x(showbase)为零值整数的标准行为,还是只是我的MinGW实现的一个怪癖?



这一切都开始于一个愉快的星期天早上...我想转储一些句柄的十六进制表示,并在一致,格式

我想要一个前导0x 和一个固定宽度,但使用预期的流操纵器,这是难以捉摸的。

我发现这样做的唯一方法是将Handles转换为unsigned long。
这似乎有点不合理,我希望我不是唯一的人曾经想过这个..

我缺少一些东西六边形操纵器?是因为类型void *(HANDLE)是简单地定义在正常的十六进制处理的ostream之外?



总之:我不想将HANDLE投射到不是的地方。

并且我不想硬编码一个0x前缀。有没有办法使用标准操纵器?或者我需要重载ostream的处理HANDLE? (但这可能会超载我!)



这是我的测试代码(及其输出)。

我使用'。填充字符,为了清楚起见,(我将实际使用'0')

  HANDLE h; 
ULONG ul;
int iH = sizeof(h); //这个void *类型的字节数。
int iW = iH * 2; //最大十六进制数(width)。
int iW2 = iW + 2; //最大十六进制数(+ 2为showbase0x)。
int iX = 4; //每十六进制数位的位数。
int ib = iH * 8; // HANDLE(指数)中的最大数位。
int i;
std :: cout<< std :: setfill('。'); //我实际上想要'0';
//用于显示清晰度的点
for(i = 0; i {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没有效果* /<
<<我想要0x<< std :: setw(iW)<< std :: hex<< std :: noshowbase<< std :: right< ;(ULONG)h
<< std :: endl;
}

// ul ......... 0 h ......... 0我想要0x ....... 0
// ul 0x ....... fh ....... 0xf我想要0x ....... f
// ul 0x ... ff h。 ..... 0xff我想要0x ... ff
// ul 0x ... fff h ..... 0xfff我想要0x ..... fff
// ul 0x .... ffff h .... 0xffff我想要0x ... ffff
// ul 0x ... fffff h ... 0xfffff我想要0x ... fffff
// ul 0x..ffffff h ..0xffffff我想要0x..ffffff
// ul 0x.fffffff h .0xfffffff我想要0x.fffffff
// ul 0xffffffff h 0xffffffff我想要0xffffffff $我在 - 这是从那里直接复制/粘贴。

所以这听起来像C ++ 98标准(通过说'使它像C的printf(%#x,0)')愚蠢的行为你看到。唯一的方法来得到你想要的是去掉std :: showbase并显式输出0x。很抱歉。


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?

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?

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!)

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
解决方案

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

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不存在为零值,内部不工作的HANDLE?我必须Ige吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 05:42