473,396 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Base 10 to Base 3 Conversion

Help. I need the algorithm for converting ,say, a base 3 numeral to base 10.
Feb 13 '07 #1
8 5812
AdrianH
1,251 Expert 1GB
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
Feb 14 '07 #2
Adrian,
Could you please provide an example for me? Thanks
Feb 15 '07 #3
Ganon11
3,652 Expert 2GB
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?
Feb 15 '07 #4
AdrianH
1,251 Expert 1GB
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.
Expand|Select|Wrap|Line Numbers
  1.         10’s column
  2.              |
  3. 100’s column |  1’s column
  4.            \ | /
  5.             210
  6.  
  • 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:
Expand|Select|Wrap|Line Numbers
  1.         3’s column
  2.              |
  3.   9’s column |  1’s column
  4.            \ | /
  5.             210
  6.  
  • 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.

Expand|Select|Wrap|Line Numbers
  1.         16’s column
  2.              |
  3. 256’s column |  1’s column
  4.            \ | /
  5.             FED
  6.  
  • 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.
Expand|Select|Wrap|Line Numbers
  1. 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
Feb 15 '07 #5
AdrianH
1,251 Expert 1GB
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
Feb 15 '07 #6
Ganon11
3,652 Expert 2GB
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.
Feb 15 '07 #7
r035198x
13,262 8TB
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
Feb 15 '07 #8
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.
Feb 15 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
7
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'...
7
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
5
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: ...
1
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...
6
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;
8
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...
3
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...
11
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>...
19
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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...
0
jinu1996
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...
0
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.