如何跨preT号码正确(十六进制,华侨城,DEC) [英] How to interpret numbers correctly (hex, oct, dec)

查看:171
本文介绍了如何跨preT号码正确(十六进制,华侨城,DEC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

用户输入:0×43,0123,65

程序输出:

  0×43进制转换为十进制67
0123八进制转换为十进制83
65(十进制)转换为65(十进制)

所以,很显然,我需要一种方法来除preT的数字,但我不知道如何去这样做。我已经试过各种方法,如阅读他们到一个函数,并将它们转换成字符串,反之亦然(见的这里 code的例子),但国米preting的数字总是需要转换成某种格式,象垃圾一样清除原来的输入。

我能想到的唯一的事情是重载一个>>操作员每次读取一个字符,如果它看到0x或0在输入的开始则将其读入之前,它存储了整个输入字符串一个int。然后程序会以某种方式有输出时,以确定正确的机械手。

不知道是否有这样做一个简单的方法,任何帮助是AP preciated。

编辑:这已经解决了,但我还是决定发布code中如果有人有兴趣

 的#includestd_lib_facilities.h无效number_sys(NUM字符串,字符串和放大器; S)
{
  如果(NUM [0] =='0'和;及(NUM [1] ='×'和;!试验#[1] =X)!)S =八进制;
  否则如果(NUM [0] =='0'和;及(NUM [1] =='X'|| NUM [1] =='X'))S =十六进制数;
  否则S =十进制;
}诠释的main()
{
    COUT<< 十六进制数字的输入,DEC,或十月使用0XX取消\\ n;
    串;    而(CIN>&胺a){
    如果(A ==0XX)打破;
    串atype的;
    number_sys(一,atype的);    INT ANUM = strtol将(a.c_str(),NULL,0);    COUT<< A<<运输及工务局局长(20-则为a.length())≤;&下; atype的<<运输及工务局局长(20)所述;&下; 转换为<<运输及工务局局长(10)
         << ANUM<<运输及工务局局长(10)所述;&下; 小数\\ N的;
                 }    keep_window_open();
}


解决方案

看看strtol函数。

 的char * ARGS [3] = {0×43,0123,65};
对(INT I = 0;我3; ++ⅰ){
  长整型值=与strtol(参数[I],NULL,0);
  的printf(%S转换到%D十进制\\ n,ARGS [I],值);
}

输出:

  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

这篇关于如何跨preT号码正确(十六进制,华侨城,DEC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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