如何正确解释数字(十六进制、八进制、十进制)
我正在尝试编写一个程序,该程序接受十六进制、八进制和十进制的输入,将它们存储在整数变量中,并将它们连同十进制形式的转换一起输出。 例如:
用户输入: 0x43, 0123, 65
程序输出:
0x43 hexadecimal converts to 67 decimal
0123 octal converts to 83 decimal
65 decimal converts to 65 decimal
显然我需要一种方法来解释这些数字,但我不确定如何去做。 我尝试了各种方法,例如将它们读入函数并将它们转换为字符串,反之亦然(请参阅 此处查看代码示例),但解释数字总是需要转换为某种格式,从而破坏原始输入。
我唯一能想到的就是重载 >> 一次读取一个字符的运算符,如果它在输入的开头看到 0x 或 0,那么它会将整个输入存储到一个字符串中,然后再将其读入一个 int 中。 然后程序必须在输出过程中以某种方式确定正确的操纵器。
不确定是否有更简单的方法来做到这一点,我们将不胜感激。
编辑:这个问题已经解决了,但我决定将代码发布到如果有人感兴趣的话。
#include "std_lib_facilities.h"
void number_sys(string num, string& s)
{
if(num[0] == '0' && (num[1] != 'x' && num[1] != 'X')) s = "octal";
else if(num[0] == '0' && (num[1] == 'x' || num[1] == 'X')) s = "hexadecimal";
else s = "decimal";
}
int main()
{
cout << "Input numbers in hex, dec, or oct. Use 0xx to cancel.\n";
string a;
while(cin >> a){
if(a == "0xx")break;
string atype;
number_sys(a, atype);
int anum = strtol(a.c_str(), NULL, 0);
cout << a << setw(20-a.length()) << atype << setw(20) << "converts to" << setw(10)
<< anum << setw(10) << "decimal\n";
}
keep_window_open();
}
I'm trying to write a program that takes input of - hexadecimals, octals, and decimals -, stores them in integer variables, and outputs them along with their conversion to decimal form. For example:
User inputs: 0x43, 0123, 65
Program outputs:
0x43 hexadecimal converts to 67 decimal
0123 octal converts to 83 decimal
65 decimal converts to 65 decimal
So obviously I need a way to interpret the numbers, but I'm not sure how to go about doing it. I've tried various methods such as reading them into a function and converting them into a string, and vice versa (see here for code examples), but interpreting the numbers always requires conversion to some format that trashes the original input.
The only thing I can think of is overloading a >> operator that reads a character at a time and if it sees 0x or 0 at the beginning of the input then it stores the whole input into a string before it is read into an int. Then the program would somehow have to determine the right manipulator during output.
Not sure if there is a simpler way to do this, any help is appreciated.
Edit: This has been solved, but I decided to post the code in if anyone is interested.
#include "std_lib_facilities.h"
void number_sys(string num, string& s)
{
if(num[0] == '0' && (num[1] != 'x' && num[1] != 'X')) s = "octal";
else if(num[0] == '0' && (num[1] == 'x' || num[1] == 'X')) s = "hexadecimal";
else s = "decimal";
}
int main()
{
cout << "Input numbers in hex, dec, or oct. Use 0xx to cancel.\n";
string a;
while(cin >> a){
if(a == "0xx")break;
string atype;
number_sys(a, atype);
int anum = strtol(a.c_str(), NULL, 0);
cout << a << setw(20-a.length()) << atype << setw(20) << "converts to" << setw(10)
<< anum << setw(10) << "decimal\n";
}
keep_window_open();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您始终可以将其存储为字符串来开始,然后查看前两个字符以查看它们是否为 0x:
You could always store it as a string to start, and look at the first two characters to see if they are 0x:
我不确定是否有 C++ 方法可以做到这一点,但如果您不介意一点 C 风格,您可以将其读入
char
数组并使用类似sscanf(缓冲区,“%i”,&输出)
。%i
根据输入的格式将输入解释为十六进制、八进制或十进制,就像您所描述的那样。编辑:啊,不知道
strtol
也可以做到这一点。 不理我。I'm not sure if there is a C++ way of doing this, but if you don't mind a little C-ishness, you can read the thing into a
char
array and use something likesscanf(buffer, "%i", &output)
. The%i
interprets the input as hex, octal or decimal depending on its format, just like you describe.Edit: Ah, didn't know that
strtol
could also do this. Ignore me.如果要保留基本信息(十六进制/八进制/十进制),则需要将该信息与整数值本身分开存储,并且需要您至少解析输入字符串的前几个字符(sscanf ()、strtol() 等不会为您保留该信息)。
您可以推出自己的迷你解析器来保存输入库并进行转换(代码来自我的脑海,未经测试):
If you want to preserve the base information (hex/oct/dec), you will need to store that information separately from the integer value itself, and it will require you to parse at least the first couple of characters of the input string (sscanf(), strtol(), etc., won't preserve that information for you).
You could roll your own mini-parser that saves the input base and does the conversion (code off the top of my head, untested):
如果您确实必须使用单个整数变量来存储显示最终输出所需的所有信息,那么您必须使用整数变量的一部分来存储原始基数输入已输入。否则无法恢复。
If you literally have to use a single integer variable for storage of all the information that you need in order to display your final output, then you have to use part of the integer variable for storing the original base that the input was in. Otherwise it's not recoverable.
看一下 strtol 函数。
输出:
Take a look at the strtol function.
Outputs: