Register to reply 
Converting between number systems 
Share this thread: 
#1
Jul2508, 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. 


#2
Jul2508, 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.



#3
Jul2508, 04:07 AM

P: 2,043




#4
Jul2508, 04:41 AM

Admin
P: 23,363

Converting between number systems
Think how log_{base}(number) relates to the number of digits.



#5
Jul2708, 06:04 AM

P: 1,017

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. Carrying this forward to other bases, if I convert to binary, then log_{2}(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? 


#6
Jul2708, 09:10 AM

Admin
P: 23,363

Do you know how to convert between logarithms of different bases? Pretty simple operation. 


#7
Jul2708, 11:04 AM

P: 1,017

The log method will only work after I've found the number out (ie, log_{2}1010 =3.something), but I cant use this before converting 10. Thats what I need to do. 


#8
Jul2708, 11:48 AM

Math
Emeritus
Sci Advisor
Thanks
PF Gold
P: 39,310

In general, the number X, in base 10, converted to base a, has log_{10}(X)/log_{10}(a)+ 1 "digits". For example, the number 1232311, base 10, converted to base 16, has 6 "digits" because log_{10}(1232311)/log_{10}(16)= 5.897... has integer part 5; 6= 5+1. And, in fact, it is easy to show that 1232311_{10}= 12CDB7_{16}, with 6 "digits". 


#9
Jul2708, 12:25 PM

P: 1,017

And thank you Borek and everyone else for helping me along! 


#10
Jul2908, 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:



#11
Jul2908, 10:32 PM

P: 1,017

Problem solved. Forgot to subtract 1 in statement *(res+leni). Should be *(res+leni1). Stupid mistake.



#12
Jul3008, 02:46 AM

Sci Advisor
P: 1,587

Or you can write
for (i = len  1; i >= 0; i) There's no rule that says your FOR loops must count up. 


#13
Jul3008, 04:01 AM

Admin
P: 23,363

Are you aware of



#14
Jul3008, 08:52 AM

Sci Advisor
HW Helper
P: 8,955




#15
Jul3008, 01:34 PM

Admin
P: 23,363

[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 log_{d}b and you are ready 


#16
Jul3008, 01:41 PM

Sci Advisor
HW Helper
P: 8,955

Somehow I managed to get [tex]a^b = x[/tex] !!! thanks  I'm sure that will come in useful one day, not sure when. 


#17
Jul3008, 01:47 PM

Admin
P: 23,363

It was much more obvious 30 years ago, when calculators were much more rare then now. Everyone knew how to convert from ln to log_{10}  by multipliying by 2.303!



#18
Jul3108, 04:57 AM

P: 1,017




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 