answersLogoWhite

0


Best Answer

The following code will convert any number in any base to any other base, from binary to hexadecimal, and everything inbetween.

#include<iostream>

#include<string>

#include<sstream>

typedef unsigned long long ull;

typedef unsigned long ul;

const std::string symbols="0123456789abcdef";

std::string inputvalue(ul base)

{

using namespace std;

string value;

while(1)

{

cout<<"Enter a positive value : ";

string s;

getline(cin,s);

if(s.size())

{

for(string::iterator i=s.begin();i!=s.end();++i)

if(*i>='A' && *i<='Z')

*i+=32;

string actual = symbols.substr(0,base);

if(s.find_first_not_of(actual)!=string::npos)

{

cout<<"The value you entered is invalid for the base.\n"

<<"Please enter another value.\n"<<endl;

continue;

}

value=s;

break;

}

}

return(value);

}

ul inputbase(std::string prompt)

{

using namespace std;

ul result=0, min=2, max=16;

while(1)

{

cout<<prompt.c_str()<<" ["<<min<<".."<<max<<"] : ";

string s;

getline(cin,s);

if(s.size())

result=stoul(s,0,10);

if(result<min result>max)

cout<<"The base must be in the range "

<<min<<".."<<max<<"\n"

<<"Please enter another base.\n"<<endl;

else

break;

}

return(result);

}

ull base2dec(std::string value,ul base)

{

ull col=1, num=0;

for(std::string::reverse_iterator i=value.rbegin(); i!=value.rend(); ++i)

{

num+=symbols.find(*i,0)*col;

col*=base;

}

return(num);

}

std::string dec2base(ull dec,ul base)

{

using namespace std;

int len=1;

ull tmp=dec;

while(tmp/=base)

++len;

string value("0",len);

while(dec)

{

value[--len]=symbols[dec%base];

dec/=base;

}

return(value);

}

int main()

{

using namespace std;

ul base=inputbase("Enter the base of the value");

string value=inputvalue(base);

ul newbase=inputbase("Enter the base to convert to");

value=dec2base(base2dec(value,base),newbase);

cout<<"New value:\t"<<value.c_str()<<endl;

return(0);

}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you convert from Decimal to base6 in c plus plus code?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the sum of the binary numbers of 1001 plus 10 in both binary and decimal?

easy, 1011. in binary of course. convert 1011 binary to decimal you get 11.


How do you convert CDIX plus XCV into a decimal numeral?

CDIX = 409 and XCV = 95, so 409 + 95 = 504 (DIV)


How do you convert 5.9 into a decimal?

The number 5.9 is already in decimal form. We know that 5.9 is 5 plus 9/10. The 9/10 is 0.9, so 5 + 9/10 is 5.9 as already written in decimal form.


How do you convert a turbo c plus plus code into Linux shell scripting code without using gcc or g plus plus compiler?

You cannot. C++ and shell script (which shell, by the way? there are more than one) are entirely different languages.


What does a negative decimal plus a negative decimal equal?

A negative decimal plus a negative decimal equals a more negative decimal


What decimal part of a whole is one half plus one fourth plus one tenth?

0.85, here's how:1/2 = 0.501/4 = 0.251/10 = 0.10Add them together to get 0.85 You could also convert the fractions to common denominators first, to get 10/20 + 5/20 + 2/20 = 17/20, then convert that to get the same decimal (0.85).


What is 800 plus 20 percent?

Find 20% of 800. Divide the percentage by 100 to convert it to a decimal. You get 0.2. Then multiply the decimal by the amount. 0.2 x 800. You get 160. Then add this to the original amount. 800 + 160 = 960. Or to simplify this...you can just multiply 800 by 1.2 (1 plus the percentage in decimal form).


What is the answer of binary decimal plus binary?

If you want to add numbers in different bases, in this case decimal and binary, or do any other calculation that involves different bases for that matter, you have to convert all numbers to a single system first - for example, all to decimal. Then you can do the operation. It is really up to you in what base you represent the final answer. In this example, you can convert back to binary, for example.


What is 1 fourth of six percent?

no that's just 6 divided by 4 plus you cant divide a percent by a fraction unless you convert both into a decimal. the answer should be .24 because if you convert 1/4 into a decimal its .25 and 6% into a decimal is .06. When you divide .06 (the original 6%) by .25 (the original 1/4 fraction) you get the answer of .24


How do you convert from assembly to binary in c plus plus?

Use inline assembly instructions. Then compile your C++ program to produce the machine code.


What is three fifths plus one half?

Convert to same types to give six tenths and five tenths. That is eleven tenths. That is one and one tenth or 1.1 in decimal.


Write as a decimal 100 plus 10 plus 1 plus 1 10?

100 plus 10 plus 1 plus 110 is 221 when expressed as a decimal to base 10.