十進位數到其他進位數的轉換

來源:互聯網
上載者:User

偶有興緻,想起敲幾句代碼,卻手生了。哎,貴在堅持呀。

先在這裡添代碼,後面解釋。

#include<stdio.h>#include<stdlib.h>#define LEN 32/* *將十進位數num轉為base進位數,base允許範圍為[2,62] */void transter(int num,int base){        if (base<2 || base>62)        {                printf("參數非法!\n");                return ;        }        char result[LEN];        int beichushu,yushu;        beichushu=num;        int i=LEN-1;        while(beichushu!=0 && i>0)        {                yushu=beichushu%base;                beichushu=beichushu/base;                if(yushu>=0 && yushu<=9)                        result[i--]=yushu+'0';                else if(yushu>=10 && yushu<=15)                        result[i--]=yushu+'A'-10;                else if(yushu>=16 && yushu<=35)                        result[i--]=yushu+'G'-16;                else if(yushu >=36 && yushu <=61)                        result[i--]=yushu+'a'-36;                else                        result[i--]=' ';        }        for(;i>=0;)                result[i--]=' ';        printf("%s\n",result);}int main(){        int num=123456;        int base;        for(base=2;base<=62;base++)        {                printf("base=%d\tresult=",base);                transter(num,base);        }        return 0;}

允許十進位數到base進位數的轉換,其中base=[2,62]。當然可以擴充其他進位數。

十進位數及符號對應表:

十進位數    符號

0    0
1    1
……    ……
9    9

10    A
11    B
……    ……
15    F

16    G
17    H
……    ……
35    Z

36    a    
37    b
……    ……
61    z

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.