十進位轉十六進位 代碼

來源:互聯網
上載者:User

 

5621轉換為16進位數的方法是“除16取餘”方法, 5621/16=商351餘5,16進位最後一位為5 351/16=商21餘15,16進位右邊第二位為F 21/16=商1餘5,16進位右邊第三位為5 1/16=商0餘1,16進位數右邊第四為為1 商0就不繼續除了  
最後的結果為15F5
#include <iostream>#include <string>using namespace std;// 整數轉換成字串string  int2str( int  num); int main(int argc, char* argv[]){// 十進位轉十六進位int num = 0;cout<<"Please Input A Num "<<endl;cin>>num;string str_num;while(num / 16 != 0){int a = num / 16;int mod_a = num % 16;cout<<"mod_a = "<<mod_a<<endl;string s;switch(mod_a){case 10:s = 'A';break;case 11:s = 'B';break;case 12:s = 'C';break;case 13:s = 'D';break;case 14:s = 'E';break;case 15:s = 'F';break;default:s = int2str(mod_a);break;}str_num = s + str_num;num = a;}if(num % 16 != 0){int last_a = num % 16;str_num = int2str(last_a) + str_num;}cout<<"The Hex Num = "<<str_num<<endl;return 0;}string  int2str( int  num){if (num  ==   0 )return   " 0 " ;string  str  =   "" ;int  num_  =  num  >   0   ?  num :  - 1   *  num;while (num_){str  =  ( char )(num_  %   10   +   48 )  +  str;num_  /=   10 ;} if (num  <   0 )str  =   " - "   +  str;return  str;}

 

聯繫我們

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