c++ - 取出用户输入整数中的每个数字,并显示每个数字到它的十六进制值的转换?
问题描述
我正在做一个程序,让用户输入一个十进制值,用户可以选择将其转换为二进制、八进制或十六进制。我得到了二进制和八进制转换,但我想做十六进制转换。我知道,在 C++ 中,我需要做的就是转换:
int convert = 78
cout << hex << convert << endl;
Output:
4e
然而,这不是我正在处理的。我想显示它,以便程序取出整数中的每个数字,并显示该数字被转换为十六进制。例如,我将向您展示我是如何进行二进制转换的。
//Variable declaration
int b;
int d;
char decision;
//Declare function prototype
int convertDecimalToBinary(int);
//Welcome the user. Then ask them to enter a decimal value. The input will be held in int d.
cout << "Welcome to The Converter!";
decimal:
cout << "\n" << "Please enter a decimal value: ";
cin >> d;
conversion:
//Ask the user if they want to conver this to Binary, Octal, or Hexadecimal
cout << "\n\nDo you want to convert " << d
<< " to Binary (b), Octal (o), or Hexadecimal (h)?: ;
cin >> decision;
//If the user wanted to do a binary conversion to the inputted decimal value
if (decision == 'b')
{
//Call the convertDecimalToBinary function, and set the value of it equal to b.
b = convertDecimalToBinary(d);
//Output the conversion
cout << "\n\nThe decimal value you inputted, " << d << ", in binary: " << b << endl;
}
/*
The convertDecimalToBinary function, this will take the user input (a decimal value) and convert it to binary.
@param decimal, the decimal value the user inputted
@return binary, the converted binary value.
*/
int convertDecimalToBinary(int decimal)
{
//Declare variables for the function
int x = decimal;
int i = 0;
int a = 1;
int remainder;
int binary = 0;
int step;
//Do the binary conversion until x=0
while (x != 0)
{
//Do the what was just explained. Set int step equal to x/2, set int remainder to x%2. Include counter addition, i++.
i++;
step = x / 2;
remainder = x % 2;
cout << "| Stage " << i << ": " << x << " / 2 = " << step << " | Remainder: " << remainder << " | " << endl;
//This is to prevent and infinite loop
x /= 2;
//This will print out the remainder digits and form it in decimal
//We need to do last remainder first, to first remainder last.
binary += remainder * a;
a *= 10;
}
//Return the conversion
return binary;
}
这是输出的屏幕截图:
Output:
Please enter a decimal value: 109
Do you want to convert 109 to Binary (b), Octal (o), or Hexadecimal (h)?: b
| Stage 1: 109 / 2 = 54 | Remainder: 1 |
| Stage 2: 54 / 2 = 27 | Remainder: 0 |
| Stage 3: 27 / 2 = 13 | Remainder: 1 |
| Stage 4: 13 / 2 = 6 | Remainder: 1 |
| Stage 5: 6 / 2 = 3 | Remainder: 0 |
| Stage 6: 3 / 2 = 1 | Remainder: 1 |
| Stage 7: 1 / 2 = 0 | Remainder: 1 |
The decimal value you inputted, 109, in binary: 1101101
老实说,八进制与此非常相似。但是你会看到一步一步的方式。我希望能够拉出一个数字,并能够将该数字转换为十六进制。显示该数字及其十六进制值,然后转到下一个数字。转换完所有数字后,返回主程序并显示十六进制!这可能吗,如果可以,我怎样才能做到这一点(如果可以的话,至少帮助我启动它)。我很感激伙计们!
解决方案
推荐阅读
- javascript - 使提交按钮也发送一个额外的值
- reporting-services - TFS 构建服务器因 Reporting Services 项目而失败
- mysql - 无法从 Intellij 连接到在 docker 容器中运行的 mySql - “指定的数据库用户/密码组合被拒绝”
- php - 搜索到的词没有在我的数据库中正确注册
- vb.net - 如何在 VB.Net 2003 中创建按钮以打开另一个 apx.net 网页
- java - 数组中的所有元素转为小写 - toLowerCase() 方法
- javascript - 如何在javascript中获取动态生成的按钮的内部文本?
- javascript - Angular中的日期时间到整数
- spring - 将 spring session JDBC 与现有数据库一起使用(不是 springboot)
- node.js - polyfills 应该是依赖项还是 devDependency?