二進位、八進位、十進位、十六進位數之間的轉換

來源:互聯網
上載者:User

標籤:

一、位元轉換成十進位數

由位元轉換成十進位數的基本做法是,把位元首先寫成加權係數展開式,然後按十進位加法規則求和。這種做法稱為"按權相加"法。

//Function to convert binary to decimalint binary_decimal(int num){    int dec = 0, base = 1, rem;    while(num > 0)    {        rem = num % 10;        dec = dec + rem*base;        base = base*2;        num /= 10;    }    return dec;}

二、十進位數轉換為位元

//Function to convert decimal to binaryint decimal_binary(int num){    int rem, base = 1, binary = 0;    while(num != 0)    {        rem = num % 2;        num /= 2;        binary += rem*i;        base *= 10;    }    return binary;}

三、位元與八位元之間的轉換

四、二進位與十六進位轉換

二進位、八進位、十進位、十六進位數之間的轉換

聯繫我們

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