Help. I need the algorithm for converting ,say, a base 3 numeral to base 10.
8 5812
Help. I need the algorithm for converting ,say, a base 3 numeral to base 10.
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.
Now instead of using 10, use 3.
There you go,
Adrian
Adrian,
Could you please provide an example for me? Thanks
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?
Adrian,
Could you please provide an example for me? Thanks
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. -
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: -
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*pow(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. -
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. - numDigit = charDigit - ’0’;
So let’s assume that the base 3 is stored in a c-string. - 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.
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?
Yeah that is correct. However, this isn't what the tomdee7 was asking for. Doh!
Adrian
Whoop,s i misread it, too. Oh well - we all make mistakes. Hopefully tomdee still got what he was looking for with your long response.
Help. I need the algorithm for converting ,say, a base 3 numeral to base 10.
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,
2 * 3^0 + 1 * 3^1 + 2 * 3^3 = 23
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,
2 * 3^0 + 1 * 3^1 + 2 * 3^3 = 23
Thank you very much. You were right on the mark.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Christian Engström |
last post by:
When I compile the below program with Microsoft Visual C++ version 6, I
get the results I expect, which is that the program should write out
base()
derived()
base()
derived(derived)
When I...
|
by: Christian Engström |
last post by:
When i compile the program listed below with gcc version 3.3.1 (MinGW on
Windows XP) I get the following result:
Calling 'func(d)':
'base' copy constructor
Calling 'func(*d_handle)':
'base'...
|
by: James Brown |
last post by:
I have two classes, a base class, and a
class base
{
public:
base();
virtual void foo() = 0;
};
class derived : public base
|
by: Gord |
last post by:
I was doing some conversion of this and that and for some reason it
occurred to me that a form might be useful for this page of conversion
explanation:
...
|
by: Mark McDonald |
last post by:
This question kind of follows on from Mike Spass’ posting 10/11/2004; I don’t
understand why you can’t declare an implicit operator to convert a base class
to a derived class.
The text...
|
by: Taran |
last post by:
Hi All,
I tried something with the C++ I know and some things just seem
strange.
consider:
#include <iostream>
using namespace std;
|
by: Raider |
last post by:
I have simple class hierarchy (without multiple inheritance):
class Base {};
class Derived : public Base {};
class DeepDerived : public Derived {};
// ... a lot of types
Is it ok to cast...
|
by: Filimon Roukoutakis |
last post by:
Dear all,
assuming that through a mechanism, for example reflexion, the Derived**
is known explicitly. Would it be legal (and "moral") to do this
conversion by a cast (probably reinterpret would...
|
by: jyck91 |
last post by:
// Base Conversion
// Aim: This program is to convert an inputted number
// from base M into base N. Display the converted
// number in base N.
#include <stdio.h>
#include <stdlib.h>...
|
by: jan.loucka |
last post by:
Hi,
We're building a mapping application and inside we're using open
source dll called MapServer. This dll uses object model that has quite
a few classes. In our app we however need to little bit...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |