Register to reply

Converting between number systems

by chaoseverlasting
Tags: converting, number, systems
Share this thread:
chaoseverlasting
#1
Jul25-08, 02:43 AM
P: 1,017
I'm writing a program to convert between the different number systems (from decimal to any base), I know what the general algorithm is and how to implement it. The problem Im facing is, that I dont know how do decide the number of digits in the converted number.

For example, if I were to convert 10 from decimal to binary, I would get 1010, but the number of digits has changed from 2 to 4. How do you decide the number of digits in the new number (1010 in this case).

I know that roughly, if youre changing to a base greater than 10, the number of digits should decrease, and if converting to a base lesser than 10, the digits should increase, but dont know by how much.
Phys.Org News Partner Science news on Phys.org
Pilot sites in energy from coffee waste show good results
Startups offer banking for smartphone users
Factor in naked mole rat's cells enhances protein integrity
Defennder
#2
Jul25-08, 02:49 AM
HW Helper
P: 2,616
There isn't any general algorithm. I had this for a lab assignment one and I made use of a different algorithm from the one the lab TAs were teaching. My algorithm converted the decimal base from front to back while theirs did the reverse. You should describe how your algorithm is supposed to work before we can help.
MeJennifer
#3
Jul25-08, 04:07 AM
P: 2,043
Quote Quote by chaoseverlasting View Post
I dont know how do decide the number of digits in the converted number.
Think of a simple formula that can determine the tallest number a given number of digits can hold in any base. For instance for 4 digits in the 10-based system it is 10 * 10 * 10 * 10 - 1 for the binary system it would be 2 * 2 * 2 * 2- 1.

Borek
#4
Jul25-08, 04:41 AM
Admin
Borek's Avatar
P: 23,585
Converting between number systems

Think how logbase(number) relates to the number of digits.
chaoseverlasting
#5
Jul27-08, 06:04 AM
P: 1,017
Quote Quote by Defennder View Post
There isn't any general algorithm. I had this for a lab assignment one and I made use of a different algorithm from the one the lab TAs were teaching. My algorithm converted the decimal base from front to back while theirs did the reverse. You should describe how your algorithm is supposed to work before we can help.
Im doing it the only way I know how, whatever number need to be converted is divided repeatedly by the base and the remainders are collected. The collected remainders in the reverse order is the number.

Im collecting the remainders in a dynamic array, and I need to know the size of the converted number to define the size of the array.


Quote Quote by Borek View Post
Think how logbase(number) relates to the number of digits.
If we use a base 10 number and find log10(number) for it, then because that number can be expressed in powers of 10, then [log[sub]10[/number](number)] (where[...] is the greatest integer function) will give us the number of digits -1 because whatever the number, without the powers of 10 its value should always be lesser than 1.
Carrying this forward to other bases, if I convert to binary, then log2(number}2 should give me number of digits of binary -1 right?

The only problem with this is that I need to know the binary number to find its length and I cant initialize the array. How can I find out the length of the number before conversion?

Quote Quote by MeJennifer View Post
Think of a simple formula that can determine the tallest number a given number of digits can hold in any base. For instance for 4 digits in the 10-based system it is 10 * 10 * 10 * 10 - 1 for the binary system it would be 2 * 2 * 2 * 2- 1.
Why does the -1 come in? I mean I get that 104 would be 5 digits, but is there a more formal reason for it?
Borek
#6
Jul27-08, 09:10 AM
Admin
Borek's Avatar
P: 23,585
Quote Quote by chaoseverlasting View Post
The only problem with this is that I need to know the binary number to find its length and I cant initialize the array. How can I find out the length of the number before conversion?
Number is a number, and its log is its log, doesn't matter if it is decimal, hexadecimal or something else.

Do you know how to convert between logarithms of different bases? Pretty simple operation.
chaoseverlasting
#7
Jul27-08, 11:04 AM
P: 1,017
Quote Quote by Borek View Post
Number is a number, and its log is its log, doesn't matter if it is decimal, hexadecimal or something else.

Do you know how to convert between logarithms of different bases? Pretty simple operation.
Yeah, thats not what I meant. What I mean is, that when Im converting between base 10 and any other base, how can I find the length of the number in the other base? Eg, 10 from decimal to binary is 1010, and the length in binary is 4. Before converting, how can I find the length in binary?

The log method will only work after I've found the number out (ie, log21010 =3.something), but I cant use this before converting 10. Thats what I need to do.
HallsofIvy
#8
Jul27-08, 11:48 AM
Math
Emeritus
Sci Advisor
Thanks
PF Gold
P: 39,553
Quote Quote by chaoseverlasting View Post
Yeah, thats not what I meant. What I mean is, that when Im converting between base 10 and any other base, how can I find the length of the number in the other base? Eg, 10 from decimal to binary is 1010, and the length in binary is 4. Before converting, how can I find the length in binary?

The log method will only work after I've found the number out (ie, log21010 =3.something), but I cant use this before converting 10. Thats what I need to do.
No! That's not what he's saying at all! 1610= 100002 because 16= 24. It has 5 bits because it is 24 and so is a 1 followed by 4 0s. 1710= 100012 because 17= 24+ 1. It has 5 bits because it is 1 (less than 2) added to 24. Or, more consisely, 16, in base 2 has 5 bits because log2(16)+ 1= 4+1 (the "1" is added for the non-zero bit) and 17, in base 2, also has 5 bits because log2(17)= log10(17)/log10(2)= 4.087... has integer part 4 and 4+ 1= 5.

In general, the number X, in base 10, converted to base a, has log10(X)/log10(a)+ 1 "digits".


For example, the number 1232311, base 10, converted to base 16, has 6 "digits" because log10(1232311)/log10(16)= 5.897... has integer part 5; 6= 5+1. And, in fact, it is easy to show that 123231110= 12CDB716, with 6 "digits".
chaoseverlasting
#9
Jul27-08, 12:25 PM
P: 1,017
Quote Quote by HallsofIvy View Post
No! That's not what he's saying at all! 1610= 100002 because 16= 24. It has 5 bits because it is 24 and so is a 1 followed by 4 0s. 1710= 100012 because 17= 24+ 1. It has 5 bits because it is 1 (less than 2) added to 24. Or, more consisely, 16, in base 2 has 5 bits because log2(16)+ 1= 4+1 (the "1" is added for the non-zero bit) and 17, in base 2, also has 5 bits because log2(17)= log10(17)/log10(2)= 4.087... has integer part 4 and 4+ 1= 5.

In general, the number X, in base 10, converted to base a, has log10(X)/log10(a)+ 1 "digits".


For example, the number 1232311, base 10, converted to base 16, has 6 "digits" because log10(1232311)/log10(16)= 5.897... has integer part 5; 6= 5+1. And, in fact, it is easy to show that 123231110= 12CDB716, with 6 "digits".
Thank you so much! I was just running around in circles trying to figure that out! That brilliantly explains it!

And thank you Borek and everyone else for helping me along!
chaoseverlasting
#10
Jul29-08, 09:56 PM
P: 1,017
I cant figure out what Im doing wrong. Could someone please tell me where Im going wrong? Here's the code:

//N-base system conversion program

#include <iostream>
#include <cmath>

using namespace std;

void convert(int , int, int, int*);    //convert(number, base, length, result)

int len=0, i;

int main()
{
    int num, base, *r;
    
    cout<<"\nEnter a number: ";
    cin>>num;
    cout<<"\nEnter base: ";
    cin>>base;
    
    len=(int)log10(num)/log10(base)+1;       //Number of digits of the converted number
    
    r=new int [len];
    
    convert(num, base, len, r);
    
    cout<<endl;
    
    for(i=0; i<len; i++)
    cout<<*(r+i);

    delete [] r;
    
    return 0;
}

void convert(int n, int b, int len, int* res)
{
     for(i=0; i<len; i++)
     {
              *(res+len-i)=n%b;
              n/=b;
     }
}
chaoseverlasting
#11
Jul29-08, 10:32 PM
P: 1,017
Problem solved. Forgot to subtract 1 in statement *(res+len-i). Should be *(res+len-i-1). Stupid mistake.
Ben Niehoff
#12
Jul30-08, 02:46 AM
Sci Advisor
P: 1,594
Or you can write

for (i = len - 1; i >= 0; i--)

There's no rule that says your FOR loops must count up.
Borek
#13
Jul30-08, 04:01 AM
Admin
Borek's Avatar
P: 23,585
Are you aware of

char *itoa( int value, char *string, int radix );
which does exactly what you need? Could be you were asked to write it on your own, but there is already a library version.
mgb_phys
#14
Jul30-08, 08:52 AM
Sci Advisor
HW Helper
P: 8,953
Quote Quote by Borek View Post
Think how logbase(number)
Is there an obvious way of calculating logbase(number) from log10(number) for any base?
Borek
#15
Jul30-08, 01:34 PM
Admin
Borek's Avatar
P: 23,585
Quote Quote by mgb_phys View Post
Is there an obvious way of calculating logbase(number) from log10(number) for any base?
We have known

[tex]a = \log_bx[/tex]

and we need

[tex]c = \log_dx[/tex]

obviously

[tex]b^a = x[/tex]

so

[tex]c = \log_db^a = a*\log_db[/tex]

but you already know a, so you just multiply by logdb and you are ready
mgb_phys
#16
Jul30-08, 01:41 PM
Sci Advisor
HW Helper
P: 8,953
Quote Quote by Borek View Post
obviously

[tex]b^a = x[/tex]
Only "obviously" if you have itthe right way round!
Somehow I managed to get [tex]a^b = x[/tex] !!!

thanks - I'm sure that will come in useful one day, not sure when.
Borek
#17
Jul30-08, 01:47 PM
Admin
Borek's Avatar
P: 23,585
It was much more obvious 30 years ago, when calculators were much more rare then now. Everyone knew how to convert from ln to log10 - by multipliying by 2.303!
chaoseverlasting
#18
Jul31-08, 04:57 AM
P: 1,017
Quote Quote by Borek View Post
Are you aware of

char *itoa( int value, char *string, int radix );
which does exactly what you need? Could be you were asked to write it on your own, but there is already a library version.
No actually! This wasnt homework, it was something I wanted to do because I need to use it in another one of my programs. I didnt know there was already something written for it! How do you use that function though?


Register to reply

Related Discussions
Converting between coordinate systems? Calculus & Beyond Homework 3
Converting a number to rectangular form Calculus & Beyond Homework 7
Converting from 2D coordinate Systems Linear & Abstract Algebra 1
Different Number Systems General Math 8
Help converting complex number to cartesian Calculus & Beyond Homework 9