4
找不到在線幫助。有什麼辦法可以解決這個問題嗎?解決std :: showbase不加前綴零
std::showbase
只(在std::hex
情況下,例如,0x
)爲非零數增加了一個前綴(如所解釋here)。我想要一個格式爲0x0
的輸出,而不是0
。
但是,僅僅使用:std::cout << std::hex << "0x" << ....
不是一種選擇,因爲右側參數可能並不總是整數(或等價物)。我要尋找一個替代showbase,這將0x
前綴0並不能扭曲非整數(或等價物),像這樣:
using namespace std;
/* Desired result: */
cout << showbase << hex << "here is 20 in hex: " << 20 << endl; // here is 20 in hex: 0x14
/* Undesired result: */
cout << hex << "0x" << "here is 20 in hex: " << 20 << endl; // 0xhere is 20 in hex: 20
/* Undesired result: */
cout << showbase << hex << "here is 0 in hex: " << 0 << endl; // here is 0 in hex: 0
非常感謝。
這真的是一個有點奇怪。即使您使用「十六進制」前綴,「0」仍以*八進制*形式打印。 :-) [0是十進制文字還是八進制文字?](http://stackoverflow.com/questions/6895522/is-0-a-decimal-literal-or-an-octal-literal) –
我是不知道我是否理解你的問題,但它是一個const int&...它不會被打印爲'hex',這只是std :: showbase的定義,正如我在OP中提到的鏈接中所解釋的那樣 – alonbe
這是一個內部玩笑,關於符合語法的事實,'0'是一個八進制數。即使你在十六進制中明確要求它,你也會得到。 –