我正在开始(真正开始)一个汇编工具,当时它只能将十进制转换为十六进制,但我想从结果中删除零。这是代码:
// HexConvert.cpp
#include <iostream>
using namespace std;
int main()
{
int decNumber;
while (true)
{
cout << "Enter the decimal number: ";
cin >> decNumber;
// Print hexadecimal with leading zeros
cout << "Hexadecimal: ";
for (int i = 2*sizeof(int) - 1; i >= 0; i--)
{
cout << "0123456789ABCDEF"[((decNumber >> i*4) & 0xF)];
}
cout << endl;
}
return 0;
}
我该怎么做?
最佳答案
你的 for 循环应该有两种状态:
- 忽略“0”的起始状态 字符,但切换到下一个 状态为非“0”
- 打印每个字符到最后。
因此,第一个状态需要在打印前检查每个字符。
关于c++ - 如何删除字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1836253/