Can anyone help me with my problem my question is how is 55 in decimal converted to 67 in octal?
Can anyone help me with my problem my question is how is 55 in decimal converted to 67 in octal?
Why those two numerals? Smells a little like an assignment.
Note that you can perform the conversion directly without using an intermediate base. Repeatedly take the remainder and integer quotient of the number (i.e. divide the number by the base). The remainders are the digits from least to greatest significance. The quotient is the number used in the next round. Stop when the number is 0 (or stop a round early when the quotient < base, using the quotient as the most significant digit). For example, consider converting the base-10 numeral 123 (often written "123₁₀"; subscripts in numerals are used to indicate base) to its base 7 equivalent.
- 123 % 7 = 4, 123 / 7 = 17. The least digit is 4.
- 17 % 7 = 3, 17 / 7 = 2. The next digit is 3.
- 2 % 7 = 2, 2 / 7 = 0. The next (and final) digit is 2
The answer: 123₁₀ = 234₇.
The same process works if neither base is a familiar one. Let's convert 123₉ to base 7. First, the beginning of the multiplication table for 7 in base 9 and (for comparison) 10:
Code:base: 9 | 10 --------+--- 1*7= 7 | 7 2*7= 15 | 14 3*7= 23 | 21 4*7= 31 | 28 5*7= 38 | 35 6*7= 46 | 42Thus 123₉ = 204₇
- A bit of long division to calculate 123₉ / 7:
So 123₉ / 7 = 15₉, 123₉ % 7 = 4Code:015₉ ← The quotient 7 ) 123₉ 12₉ - 7 --- 43₉ -38₉ = 5 * 7 --- 4 ← The remainder- 15₉ / 7 = 2, 15₉ % 7 = 0
The reason this works becomes obvious if you examine what the process does to a numeral in the base you're converting to. The number M is represented in base b using the sum M = Σ aᵢ*bⁱ = a_n * bⁿ + ... + a₁ * b¹ + a₀ * b⁰, where the coefficients aᵢ are the digits of the numeral. Note the summation, like conversion, is representation-neutral; they work in any numeric representation. Conversion is easiest in base [i]b[/b], for if you examine the numeral in base b, you'll see that the remainder is the least significant digit and division by b is a right shift. The general process is:
- M = Σ aᵢ*bⁱ = a_n * bⁿ + ... + a₁ * b¹ + a₀ * b⁰
- M₁ = Σ aᵢ*bⁱ⁻¹ = a_n * bⁿ⁻¹ + ... + a₂ * b¹ + a₁ * b⁰; next digit: a₀
- M₂ = Σ aᵢ*bⁱ⁻² = a_n * bⁿ⁻² + ... + a₃ * b¹ + a₂ * b⁰; next digit: a₁
- ...
(Note: I use "_n" for a subscripted "n" because the Unicode "subscript n" character, ₙ, isn't recognized on any computer I tested; it's not in any font, and it's not even listed in the system character viewers.)
Or, using base b numerals:
- Start: M = a_n ... a₃ a₂ a₁ a₀
- M₁ = a_n ... a₃ a₂ a₁; next digit: a₀
- M₂ = a_n ... a₃ a₂ ; next digit: a₁
- ...
Isn't working with different bases easy?
See also the many questions about bases from Ask Dr. Math.
Last edited by misson; 09-07-2011 at 07:29 AM.
Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.Misson, not Mission.
“Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
"It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)
Mission isnt Octal Base 8 rather than Base 7? From what I learned Septenary is Base 7 and Octal is Base 8
Last edited by Darkmere; 09-07-2011 at 10:53 AM.
Darkmere -- misson's example was about computing a representation of a number in any arbitrary base, and he specifically said that the first example was to convert to base 7. (The introductory paragraph can be roughly translated as, "I'm not going to do your homework for you.") The point was to give the general method, not the solution to a specific problem. Teaching a man to fish, so to speak, rather than throwing a halibut his way.
“Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
"It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)
Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.Misson, not Mission.
Yeah I was surprised that the [sub] and [sup] bb code doesn't work either
Just on the topic of Octal and Decimal, a joke I heard courtesy of my computer science professor
"Real programmers always confuse Halloween and Christmas because Oct31 == Dec25" - Andrew Rutherford
This made so much more sense after reading these posts about converting decimal to octal
Thanks for making me laugh!
“Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
"It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)