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);
}
Divide it by 1000.
Use inline assembly instructions. Then compile your C++ program to produce the machine code.
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 <cstdint> 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<uint8_t> 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
The A Plus Program is an initiative, not a test. So no, there is no answer book.
Every C plus plus program that is a main program must have the function 'main'.
Expressed in octal, the decimal sum 27 + 31 = 58 would be expressed: 33 + 37 = 72
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.
Divide it by 1000.
Use inline assembly instructions. Then compile your C++ program to produce the machine code.
easy, 1011. in binary of course. convert 1011 binary to decimal you get 11.
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')
To convert a decimal to a fraction, we first identify the decimal as a whole number plus a decimal part. In this case, 14 is the whole number and 0.6666667 is the decimal part. To convert the decimal part to a fraction, we can write it as 6666667/10000000. Combining the whole number and the fraction part, we get 14 6666667/10000000 as the fraction form of 14.6666667.
CDIX = 409 and XCV = 95, so 409 + 95 = 504 (DIV)
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.
A negative decimal plus a negative decimal equals a more negative decimal
#include<stdio.h> #include<stdlib.h> main() { int number,binary[10000],b=0; printf("Enter decimal number "); scanf("%d",&number); printf("\nBinary: "); for(;number;number/=2,b++) binary[b]=number%2; for(b--;b>-1;b--) printf("%d ",binary[b]); }
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).