如何正确解释数字(hex,oct,dec) [英] How to interpret numbers correctly (hex, oct, dec)

查看:461
本文介绍了如何正确解释数字(hex,oct,dec)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个程序,它接受 - 十六进制,八进制和小数 - 的输入,将它们存储在整数变量中,并将它们与转换为十进制形式一起输出。例如:



用户输入:0x43,0123,65



程序输出:



0x43十六进制转换为67十进制
0123八进制转换为83十进制
65十进制转换为65十进制

很明显,我需要一种解释数字的方法,但我不知道如何去做。我已经尝试了各种方法,如读取它们到一个函数,并将它们转换为字符串,反之亦然(参见这里代码示例),但是解释数字总是需要转换为一些格式,原始输入。



我唯一可以想到的是重载一个>>运算符,一次读取一个字符,如果它在输入的开头看到0x或0,将整个输入存储在字符串中,然后再读入int。然后程序会以某种方式在输出期间确定正确的操纵器。



不知道如果有一个更简单的方法来做到这一点,任何帮助是赞赏。

编辑:这已经解决了,但如果有人感兴趣,我决定发布代码。

 code> #includestd_lib_facilities.h

void number_sys(string num,string& s)
{
if(num [0] =='0' &&&&(num [1]!='x'&&#1]!='X'))s =octal
else if(num [0] =='0'&&(num [1] =='x'|| num [1] =='X'))s =十六进制
else s =decimal;
}

int main()
{
cout<< 输入十六进制,十进制或十进制数字。使用0xx取消。\\\
;
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)<< 转换为< setw(10)
< anum setw(10)<< decimal\\\
;
}

keep_window_open();
}


解决方案

  char * args [3] = {0x43,0123,65}; 
for(int i = 0; i <3; ++ i){
long int value = strtol(args [i],NULL,0)
printf(%s转换为%d decimal\\\
,args [i],value);
}

输出:

  0x43转换为67十进制
0123转换为83十进制
65转换为65十进制


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();
}

解决方案

Take a look at the strtol function.

char * args[3] = {"0x43", "0123", "65"};
for (int i = 0; i < 3; ++i) {
  long int value = strtol(args[i], NULL, 0);
  printf("%s converts to %d decimal\n", args[i], value);
}

Outputs:

0x43 converts to 67 decimal
0123 converts to 83 decimal
65 converts to 65 decimal

这篇关于如何正确解释数字(hex,oct,dec)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆