answersLogoWhite

0

Binary to octal numbers in c plus plus codes?

Updated: 10/24/2022
User Avatar

Jayjuna

Lvl 1
10y ago

Best Answer

The following code demonstrates one way to convert from binary to octal based on user input:

#include<iostream>

#include<string>

typedef unsigned int uint;

std::string input(std::string prompt)

{

std::cout<<prompt<<": ";

std::string s;

std::getline(std::cin,s);

return(s);

}

uint input_binary( uint min, uint max )

{

uint result=0;

bool repeat=true;

while(repeat)

{

result=0;

repeat=!repeat;

std::string s=input("Enter a binary value");

for(uint i=0; i<s.size() && !repeat; ++i)

{

const char c=s.at(i);

if(c<'0'c>'1')

{

std::cout<<"Binary values may only contain characters '0' or '1'\n"<<std::endl;

repeat=!repeat;

}

result<<=1;

result|=(c-'0');

}

if(!repeat)

{

if(result<min result>max)

{

std::cout<<"The binary value must be in the decimal range "<<min<<".."<<max<<"\n"<<std::endl;

repeat=!repeat;

}

}

}

return( result );

}

int main()

{

uint binary = input_binary(0x1,0xffffff);

uint temp=binary;

uint len=1;

while(temp>>=3)

++len;

std::string octal(len, '0');

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

{

*i=(binary & 0x7)+'0';

binary>>=3;

}

std::cout<<"Octal: "<<octal.c_str()<<"\n"<<std::endl;

return(0);

}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Binary to octal numbers in c plus plus codes?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is 1 plus 1 in binary?

1 + 1 = 10 in binary numbers.


Why there is need to study octal and hexidecimaln the digital machine understands only binary code?

Because the octal number sytem is more useful for writing and clearer to read. Also, we're only using the binary system since the invention of computers which is not that long ago. Before that, there was no reason to use a binary system which is again not easy to read.


What is the sum of binary numbers 10111 plus 101111?

It is 1000110.


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 the binary numbers for 10111 plus 101111?

1000110. In decimal, the sum is 23+47=70.


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.


Do the complex numbers for a group under binary operation ' plus '?

Yes, the complex numbers, as well as the real numbers which are a subset of the complex numbers, form groups under addition.


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 the decimal sum of 27 plus 31 octal?

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


Can C plus plus read binary?

yes it can very much so read binary.


What is the answer of this 1011 plus 1001 equals?

The sum of 1,011 and 1,001 is 2,012. Unless they are binary numbers, in which case 1011 + 1001 = 10100


In the binary system what is 1 plus 1?

1 plus 1 = 2