- #1
- 292
- 0
I just studied BCD, excess 3 and other codes but I guess these were used earlier. What is the current method to convert decimal to binary and back to decimal?
Can decoders and encoders be used?
Can decoders and encoders be used?
Yep.This is how we do it. Do computers do the same thing?
You can divide a number in binary format by any other whole number (not 0 though) and get the remainder (i.e. the modulus) and the quotient (i.e. the result).How? Lets say we give the number 1206 as input. But first computer needs to convert it to binary. So it must divide it by two and check the remainders. But how will it perform division on a decimal number since it is only designed to perform arithmetic on binary numbers.
Depends on the computer and the application. In the case of mainframes and other computers used for accounting type applications, the data is kept in decimal as BCD strings and the math is performed on those BCD strings, to eliminate any rounding issues due to conversion to binary and back. COBOL is an example of a high level language that includes BCD and binary based math. The cpus on a PC include basic add and subtract operations for BCD, which can be the basis for doing BCD based math on a PC.I just studied BCD ... What is the current method to convert decimal to binary and back to decimal?
One way to to this is to calculateLets say we give the number 1206 as input. But first computer needs to convert it to binary.
template<class Num>bool ParseUnsignedBase10(const char** p, const char* end, Num* res)
{
const char* p1 = *p;
Num v = 0;
while(p1 < end){
if(*p1<'0' || *p1>'9') break;
Num v1 = v*10 + (*p1 - '0');
if(v1 < v)break;
v = v1;
};
if(p1 == *p)return false;
*p = p1;
*res = v;
return true;
}