Decimal number to n binary C + + implementation

Source: Internet
Author: User

write an algorithm that converts a non-negative decimal integer N to another b -ary integer that has a base of b .

#include <iostream>

#include <string.h>

using namespace Std;

The desired decimal-to - B -binary function

void Shift (int b,int before)

{

Char B_num[b];

if (b<10) {for (int i=0;i<b;i++) b_num[i]=i+48;}

Else

{

for (int i=0;i<b;i++) b_num[i]=i+48;

for (int j=10;j<b;j++) b_num[j]= ' A ' +j-10;

}//defines the symbol for each position that is greater than ten

String result= "";

int N;int m;

while (BEFORE>=B)

{

n=before/b;

M=before%b;

RESULT+=B_NUM[M];

Before=n;

}

Result+=b_num[n];

cout<< " converted successfully , The result is :" <<endl;

for (int i=result.length () -1;i>=0;i--) cout<<result[i];

cout<<endl;

}

int main ()

{

L:int Num;int B;

cout<< " to convert decimal numbers to how many binary numbers ?" <<endl;

cin>>b;

cout<< " Please enter raw data " <<endl;

cin>>num;

Shift (B,num);

Goto L;

return 0;

}

The experimental results are as follows:

Decimal number to n binary C + + implementation

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.