编程练习——进制转换

——16进制转10进制

C:

#include <stdio.h>
 
int main(void) {
    int num = 0;
    while (~scanf ("%x", &num))
        printf("%d\n",  num);
    return 0;
}

C++ 1:

#include <iostream>  
#include <stdlib.h>  
  
using namespace std;  
  
int main()  
{  
    char str[100];  
    char *endptr;  
    while(cin>>str)  
    {  
        long number=strtol(str,&endptr,16);  
        cout<<number<<endl;   
    }  
    return 0;  
}  

C++ 2:

#include<iostream>
#include<string>
 
using namespace std;
 
int main(){
    string str;
    while(cin>>str){
        int len = str.size();
        long long value = 0;
        for(int i = 2; i<len; ++i){
            if(str[i] == 'A'){
                value = value* 16 + 10;
            }
            else if(str[i] == 'B'){
                value = value* 16 + 11;
            }
            else if(str[i] == 'C'){
                value = value* 16 + 12;
            }
            else if(str[i] == 'D'){
                value = value* 16 + 13;
            }
            else if(str[i] == 'E'){
                value = value* 16 + 14;
            }
            else if(str[i] == 'F'){
                value = value* 16 + 15;
            }
            else{
                value = value * 16 +(str[i]-'0');
            }
        }
        cout<<value<<endl;
    }
}

Python :

while 1:
    try:
        str=raw_input()
        out=int(str,16)
        print out
    except:
        break



发布了33 篇原创文章 · 获赞 39 · 访问量 14万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览