zoukankan      html  css  js  c++  java
  • 10进制与17进制的转化(代码已测试)

    代码如下:

    #include "stdafx.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    //10进制转换为17进制
    string TenTo17(int M)
    {
        string str="";
        int n;
        char a;
        char b='+';  //b用于记录符号
        if(M<0)
        {
            M=-M;
            b='-';
        }
        while(M!=0)
        {
            
            n=M%17;
            M=M/17;
            if(n>=0&&n<=9)
            {
                a=n+'0';
            }
            else if(n>=10&&n<=16)
            {
                a=n-10+'A';
            }
            str.insert(0,1,a); //表示在str的位置0处插入1个字符a
        }
        if(b=='-')
        {
            str.insert(0,1,b);
        }
        return str;
    
    
    }
    
    //17进制转换为10进制(使用string)
    int SeventeenTo10(string str)
    {
        int len=str.length();
        int sum=0;
        int temp=0;
        char b='+';
        for(int i=0;i<len;i++)
        {
            if(str[0]=='-')
            {
                b='-';
            }
            if(str[i]>='0'&&str[i]<='9')
            {
                temp=str[i]-'0';
            }
            else if(str[i]>='A'&&str[i]<='G')
            {
                temp=str[i]-'A'+10;
            }
            sum=sum*17+temp;
        }
        if(b=='-')
        {
            sum=-sum;
        }
        return sum;
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        
        unsigned int M=17456;
        cout<<"初始数据:"<<M<<endl;
        string str=TenTo17(M);
        cout<<"10进制转换为17进制:"<<str<<endl;
        int N;
        N=SeventeenTo10(str);
        cout<<"17进制转换为10进制:"<<N<<endl;
        char *str1= (char*)&str;
        return 0;
    }
  • 相关阅读:
    A Famous City
    A Famous ICPC Team
    配置单元测试环境,找不到SenTestingKit
    linux解压.tar命令
    语音输入——科大讯飞
    查看dsym错误信息
    工程里关闭arc
    导入签名错误
    mac显示隐藏文件
    类uialertview弹出动画
  • 原文地址:https://www.cnblogs.com/mangci/p/3243092.html
Copyright © 2011-2022 走看看