C + + conversion (decimal binary, octal, arbitrary)

Source: Internet
Author: User

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)) C9/>cout << "1";
		else
			cout << "0";
	}
}

int main ()
{
	printbinary (1024);
	return 0;
}

Decimal Turn octal

Decimal transfer 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 arbitrary system:

Decimal conversion to arbitrary feed source
#include <iostream>
using namespace std;

int main ()
{
	long n;
	int p,c,m=0,s[100];
	cout<< "Enter the number to convert:" <<endl;
	cin>>n;
	cout<< "Enter the system to convert:" <<endl;
	cin>>p;

	cout<< "(" <<n<< ") 10=" << "(";

	while (n!=0)//conversion, the result is stored in the array s[m]
	{
		c=n%p;
		n=n/p;
		M++;s[m]=c;   Put the remainder in order into 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]+55);
		else         //Otherwise direct output digital
			cout<<s[k];
	}

	cout<< ")" <<p<<endl;

	return 0;
}

implements eight, hexadecimal output through library functions:

#include <iostream>
using namespace std;

int main ()
{
	int test=64;
	cout<< "DEC:" <<test<<endl;
	cout<< "OCT:" <<oct<<test<<endl;//octal
	cout<< "HEX:" <

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.