C + + binary conversion (decimal to binary, octal, discretionary)

Source: Internet
Author: User
Tags decimal to binary

Decimal Turn binary:

Decimal to Binary #include<iostream>using namespace std;void printbinary (const unsigned int val) {for (int i = +; I >= 0; i --) {if (Val & (1 << i) cout << "1"; Elsecout << "0";}} int main () {printbinary (1024x768); return 0;}

Decimal Turn octal

Decimal to octal # include <iostream> #include <vector>using namespace Std;int main () {cout<< "input a number:" <<endl;int d;vector<int> Vec;cin>>d;while (d) {vec.push_back (d%8);d =D/8;} cout<< "The result is:" <<endl;for (Vector<int>::iterator ip=vec.end () -1;ip>=vec.begin ();) {cout <<*ip--;} Cout<<endl;return 0;}

Decimal to Random binary:

Decimal conversion to discretionary source code # include <iostream>using namespace Std;int main () {Long N;int p,c,m=0,s[100];cout<< " Enter the number you want to convert: "<<endl;cin>>n;cout<<" Enter the binary to convert: "<<endl;cin>>p;cout<<" ("<<n << ") 10=" << "("; while (n!=0)//", the result is stored in the array s[m]{c=n%p;n=n/p;m++;s[m]=c;   The remainder is sequentially deposited into the array s[m]}for (int k=m;k>=1;k--)//output converted sequence {if (s[k]>=10)//If 16 is the output of the corresponding letter cout<< (char) (s[k]+ else         //Otherwise direct output digital cout<<s[k];} cout<< ")" <<p<<endl;return 0;}

Eight hexadecimal output via library function:

#include <iostream>using namespace Std;int main () {int test=64;cout<< "DEC:" <<test<<endl;cout << "OCT:" <<oct<<test<<endl;//octal cout<< "HEX:" <
 
 
 
 
 
 
 
 
 
pre>

C + + binary conversion (decimal to binary, octal, discretionary)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.