Help. I need the algorithm for converting ,say, a base 3 numeral to base 10.
Base 10 to Base 3 Conversion
Collapse
X
-
Well think about it, the value of a mod 10 is the 1's column. div that value by 10. Now mod that value by 10 and you get the 10's column. Keep repeating till value is 0.Originally posted by tomdee7Help. I need the algorithm for converting ,say, a base 3 numeral to base 10.
Now instead of using 10, use 3.
There you go,
Adrian -
Converting 314 to base 3. 314 % 3 == 2, and 314 / 3 == 104 (truncated).
Now 104 % 3 == 2, and 104 / 3 == 34 (truncated).
34 % 3 == 1, and 34 / 3 == 11 (truncated).
11 % 3 == 2, and 11 / 3 == 3 (truncated).
3 % 3 == 0, and 3 / 3 = 1.
1 % 3 == 1, and 1 / 3 = 0 (truncated).
So 314 in base 3 is 102122 (first number is last mod result, second number is second to last mod result, etc.).
Can someone check this for me?Comment
-
Oops, I reread your question, I assumed that the number was in base 10 already and you were converting to base 3. To convert the other way I will have to review the parts of a number for base 10.Originally posted by tomdee7Adrian,
Could you please provide an example for me? Thanks
Code:10’s column | 100’s column | 1’s column \ | / 210- 210 can be rewritten as 200 + 10 + 0.
- And from there we can break it down as (2*100)+(1*10)+ (0*1).
- And even further as (2*pow(10,2))+( 1*pow(10,1))+(0 *pow(10,0)).
pow(x,y) where means x to the yth power.
This works for any base system. Say we use base 3 for the previous example:
Code:3’s column | 9’s column | 1’s column \ | / 210- 210(base3) can be rewritten as 200(base3) + 10(base3) + 0(base3).
- And from there we can break it down as (2*9)+(1*3)+(0* 1).
- And even further as (2*pow(3,2))+(1 *pow(3,1))+(0*p ow(3,0)).
This is slightly easier when you are dealing with a base that is smaller than the base that you normally use (which is base 10). For example, in hexadecimal (base 16), you would have to convert the digits to their equivalent value in you regular base.
Code:16’s column | 256’s column | 1’s column \ | / FED- FED(obviously base16 so I won’t mention it further) can be rewriten as F00 + E0 + D.
- And from there we can break it down as (F*256)+(E*16)+ (D*1).
- And even further as (F*pow(16,2))+( E*pow(16,1))+(D *pow(16,0)).
- But the digits F, E, and D don’t have meaning in base 10 so we must convert them to base 10 like so: (15*pow(16,2))+ (14*pow(16,1))+ (13*pow(16,0)).
So the easiest way of converting one base to another is to take the least significant digit, take that digit’s value and multiply it by the pow(base, distance from 1’s column). Keep looping till you have nothing left to convert.
Now, if the base 3 number is in a c-string, it is stored in ASCII. To convert a digit to a value, you will have to subtract from the ASCII value the ASCII value of zero.
I.e.So let’s assume that the base 3 is stored in a c-string.Code:numDigit = charDigit - ’0’;
- Create a variable to hold the converted number and initialise it to zero. Lets call that variable "number".
- Create a variable to hold the exponential position you are at. This is how far away you are from the 1’s position. Call this variable “exponent”.
- Find out the length of that string.
- Take the least significant digit from that string.
- Multiply digit by pow(3, exponent)
- Have you read in all of the digits? No? Get the next most significant digit, add 1 to exponent and go back to step 5. Otherwise your conversion is complete.
If you have any questions with what I am saying, quote the section and let me know what you are having problems with.
If you are asking for the source code, forget it. That is not why we are here. Give it a shot, post your attempt (please put [code][/code] markers around it) and say what problems you are having. We will be glad to try and give you a hand.
Hope this helps.
AdrianComment
-
Yeah that is correct. However, this isn't what the tomdee7 was asking for. Doh!Originally posted by Ganon11Converting 314 to base 3. 314 % 3 == 2, and 314 / 3 == 104 (truncated).
Now 104 % 3 == 2, and 104 / 3 == 34 (truncated).
34 % 3 == 1, and 34 / 3 == 11 (truncated).
11 % 3 == 2, and 11 / 3 == 3 (truncated).
3 % 3 == 0, and 3 / 3 = 1.
1 % 3 == 1, and 1 / 3 = 0 (truncated).
So 314 in base 3 is 102122 (first number is last mod result, second number is second to last mod result, etc.).
Can someone check this for me?
AdrianComment
-
Haven't read the other guys' posts yet but to convert a number from base n to base 10, you just start with the right most digit and multiply it n^0 and add then move to the next digit (to the left) and multiply that by that n^1, ... so that you multipy the mth digit from the right by n^(m-1), until all the digits are done. Then simply add up all these values and that's your base 10 value. so for converting 212(base 3) to base 10,Originally posted by tomdee7Help. I need the algorithm for converting ,say, a base 3 numeral to base 10.
2 * 3^0 + 1 * 3^1 + 2 * 3^3 = 23Comment
-
Thank you very much. You were right on the mark.Originally posted by r035198xHaven't read the other guys' posts yet but to convert a number from base n to base 10, you just start with the right most digit and multiply it n^0 and add then move to the next digit (to the left) and multiply that by that n^1, ... so that you multipy the mth digit from the right by n^(m-1), until all the digits are done. Then simply add up all these values and that's your base 10 value. so for converting 212(base 3) to base 10,
2 * 3^0 + 1 * 3^1 + 2 * 3^3 = 23Comment
Comment