| New Reply |
How computer converts decimal-to-binary-to-decimal |
Share Thread | Thread Tools |
| Sep1-12, 01:05 PM | #1 |
|
|
How computer converts decimal-to-binary-to-decimal
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? |
| Sep1-12, 08:58 PM | #2 |
|
|
Hey Avichal and welcome to the forums.
The standard algorithm is known as the DIV/MOD algorithm in programming. Here is an intuitive idea of how it works: http://www.mathsisfun.com/base-conversion-method.html |
| Sep1-12, 10:04 PM | #3 |
|
|
This is how we do it. Do computers do the same thing?
|
| Sep1-12, 10:05 PM | #4 |
|
|
How computer converts decimal-to-binary-to-decimal |
| Sep2-12, 01:45 AM | #5 |
|
|
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.
|
| Sep2-12, 03:28 AM | #6 |
|
|
In C++ the modulus is % and integer division is just /. If you want to make sure you get the right answer just calculate the modulus, subtract that from the value and then do the division and you are guaranteed to get the right answer. Doesn't matter if its 10358 / 2 or 10358 / 23, it's the same kind of operation. |
| Sep2-12, 06:36 AM | #7 |
|
Recognitions:
|
Otherwise, conversion is done by division (and using remainder) or multiply, depending if converting from another base to binary or from binary to another base, or if converting the integer or fractional (the part to the right of the decimal or binary point) portion of a number. |
| Sep2-12, 02:32 PM | #8 |
|
|
(((((1 × 10) + 2) × 10) + 0) × 10) + 6The computer already knows what 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10 are in binary -- they have been worked out in advance and stored in memory -- and so can perform the above calculation in binary. |
| Jan14-13, 09:41 AM | #9 |
|
|
How do you calculate e.g 321 decimal form to now how many bytes you can store?
|
| Feb7-13, 10:51 PM | #10 |
|
|
Remember, that decimal data usually present only in text, and task is about parsing.
It's easier to show C++ func doing this stuff: Code:
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;
}
|
| New Reply |
| Thread Tools | |
Similar Threads for: How computer converts decimal-to-binary-to-decimal
|
||||
| Thread | Forum | Replies | ||
| Decimal to binary, Decimal to Octal confusion! | Electrical Engineering | 10 | ||
| Hexa to Decimal Conversion, Binary to Decimal Conversion? | Calculus & Beyond Homework | 2 | ||
| Converting Decimal to Binary | Electrical Engineering | 3 | ||
| converting decimal to binary | Calculus & Beyond Homework | 5 | ||
| Decimal to Binary | General Math | 3 | ||