c-八進位 轉 十進位

來源:互聯網
上載者:User

標籤:

概述

  其實x進位轉十進位的演算法都差不多,不過如果是針對於字元形式,他們卻有點不同.使用指標和數組的形式計算,又不同.這裡示範將字元型的數組形式的八進位轉成十進位:

 1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4  5 #define MAX  10 6  7 //初始化數組為八進位. 8 void 9 init(char *octal) {10     gets(octal);11 }12 13 //將八進位轉為十進位.14 int15 octalToDecimal(char *octal) {16     int len = strlen(octal);    //實際長度.17     int sum = 0;    //儲存結果.18     int r = 0;    //指數.19     for(int i = len - 1; i >= 0; i--)20         sum += pow((float)8, r++) * (octal[i] - ‘0‘);21     return sum;22 }23 24 /*25 八進位轉十進位.26 */27 int28 main(void) {29     char octal[MAX];30     gets(octal);31     printf("r=%d", octalToDecimal(octal));32     printf("\n");33 }

 

c-八進位 轉 十進位

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.