十进制转十六进制时,如何保留开头的前导零? c++ [关闭]

问题描述 投票:0回答:0

如果我输入 -4094,我的输出将如下所示。

[Hexadecimal:   fffff002]

我试图为我的十六进制输出保留前导零。前导零是指输出左侧的额外“f”字符。 我的结果应该如下。

[Hexadecimal:   fffffffffffff002]

我怎样才能做到这一点?

这是我的代码。

#include <iostream>     //Header needed for input/output operations
#include <iomanip>      //Header needed for hexadecimal output
#include <sstream>      //Header needed for stringstream type
using namespace std;

int main()
{
 int num;   
cout << "Please enter number to convert: ";
cin >> num;
 stringstream ss;
 ss << hex << (num);
 string res(ss.str());
 cout << " [Hexadecimal:   " << res << "]\n\n";

 return 0;
}
c++ string hex converters
© www.soinside.com 2019 - 2024. All rights reserved.