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

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C plus plus Program that will convert from decimal to octal?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you write program to convert meter to kilometer in c plus plus?

Divide it by 1000.


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.


How do you write bytes in Turbo C plus plus header files?

Bytes can be written using hexadecimal, octal or decimal notation. A numeral with no prefix is always regarded as decimal. If prefixed with a leading zero it is deemed octal and if prefixed with 0x it is deemed hexadecimal. The following shows the three ways to write the decimal value 255: 255 (decimal) 0377 (octal) 0xff (hexadecimal) Hexadecimal is the generally the most convenient method of notation since each hexadecimal digit represents exactly 4-bits (a half byte or a nybble). An octal digit represents exactly 3 bits and is useful for notating bytes in groups of 3 bits. A 24-bit integer is both a multiple of 3 and 4 so it can be notated using 8 octal digits or 6 hexadecimal digits. Individual bytes are best stored using the uint8_t alias (defined in the &lt;cstdint&gt; standard library header) as this guarantees an 8-bit byte in the positive range 0 to 255 decimal. To store several contiguous bytes, use a vector of uint8_t: std::vector&lt;uint8_t&gt; bytes; bytes.push_back (255); bytes.push_back (0377); bytes.push_back (0xff); The above example pushes the value 255 onto the back of the vector three times, using decimal, octal and hexadecimal notation. You can also write contiguous bytes in multiples of 2, 4 and 8 bytes using uint16_t, uint32_t and uint64_t aliases respectively. Thus if you need a 64-bit value, use the uint64_t alias. uint64_t word = 0xffffffffffffffff; // maximum value


Is there an answer book for the A plus program?

The A Plus Program is an initiative, not a test. So no, there is no answer book.


What is the only function all C plus plus programs must contain?

Every C plus plus program that is a main program must have the function 'main'.

Related questions

What is the decimal sum of 27 plus 31 octal?

Expressed in octal, the decimal sum 27 + 31 = 58 would be expressed: 33 + 37 = 72


What is the sum of 603 plus 300 plus 562 plus 461 using octal addition?

603 + 300 + 562 + 461 = 2346 (All values and total in Octal) 387 + 192 + 370 + 305 = 1254 (All values converted from octal to Decimal) 603 + 300 + 562 + 461 = 1926 (All values decimal) Octal equivalent is of the total is 3606.


How do you write program to convert meter to kilometer in c plus plus?

Divide it by 1000.


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 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.


What is 3 6?

3+6=9 in base 10 (decimal) arithmetic. If you were in base-8 (octal) then 3 + 6 = 118. Where 118 still represents 'nine', though (it is 1 'eight' plus 1 'one')


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.


C plus plus program that convert decimal to binary using the concept of stack?

#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; main() { int number,binary[10000],b=0; printf("Enter decimal number "); scanf("%d",&amp;number); printf("\nBinary: "); for(;number;number/=2,b++) binary[b]=number%2; for(b--;b&gt;-1;b--) printf("%d ",binary[b]); }


What does a negative decimal plus a negative decimal equal?

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


How do you convert 2.9166666667 into a fraction?

To convert a decimal into a fraction, we need to look at the decimal places. In this case, 2.9166666667 can be written as 2 whole units plus 0.9166666667. Since there are 10 decimal places in 0.9166666667, we can write it as 9166666667/10000000000. Therefore, the fraction equivalent of 2.9166666667 is 2 9166666667/10000000000.


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).