AutoHotkey Community

It is currently May 26th, 2012, 4:37 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: March 28th, 2009, 7:37 pm 
Offline

Joined: December 17th, 2007, 6:39 pm
Posts: 235
Location: Galati, Romania
These simple functions convert a number in base 10 into base n and back provided that n is between 2 and 9. Maybe I'll make a function to convert the numbers into bases greater than 10 in the future.

Code:
DecToBase(number,base)
{
if (base<2) or (base>9)
return ERROR

while number>0
{
rest:=mod(number,base)
Result:=rest . Result
number-=rest
number/=base
}
return %Result%
}

BaseToDec(number,base)
{
if (base<2) or (base>9)
return ERROR

pos:=strlen(number)
power:=1
while pos>0
{
LastDigit:=SubStr(number,pos,1)
Result+=LastDigit*power
power:=power*base
pos--
}
return %Result%
}


Run this in order to test if the functions are working fine:
Code:
msgbox % BaseToDec(DecToBase(10,2),2)


Comments and suggestions are welcome!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2009, 9:20 pm 
Offline
User avatar

Joined: May 5th, 2007, 7:24 pm
Posts: 1240
Location: Seville, Spain
I've written my own version of the functions:
Code:
Dec2Base(n, b){
   if((b < 2) || (b > 9))
      return ""
   while n > 0
      r := mod(n, b), o := r o, n := (n - r) // b
   return o
}

Base2Dec(n, b){
   if((b < 2) || (b > 9))
      return ""
   o := 0
   loop % StrLen(n)
      o += SubStr(n, StrLen(n) - A_Index + 1, 1) * b ** (A_Index-1)
   return o
}

Improvements:
  • They return an empty string ("") on error
  • They are shorter
  • They abuse the comma operator
  • They heavily relie on expressions

If anybody discovers a bug, let me know that.
HTH

_________________
fincs
Highly recommended: AutoHotkey_L (see why) (all my code snippets require it)
Formal request to polyethene - I support the unity of the AutoHotkey community
Get SciTE4AutoHotkey v3.0.00 (Release Candidate)
[My project list]


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2009, 10:00 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
you could try recursion, too, like this:
Code:
ToBase(n,b) { ; n >= 0, 1 < b <= 36
   Return (n < b ? "" : ToBase(n//b,b)) . ((d:=mod(n,b)) < 10 ? d : Chr(d+55))
}

Edit: here is the counterpart, too, in recursive form
Code:
FromBase(s,b) { ; convert base b number s=strings of 0..9,A..Z, to AHK number
   Return (L:=StrLen(s))=0 ? "":(L>1 ? FromBase(SubStr(s,1,L-1),b)*b:0) + ((c:=Asc(SubStr(s,0)))>57 ? c-55:c-48)
}


Last edited by Laszlo on March 28th, 2009, 10:48 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2009, 10:16 pm 
Offline
User avatar

Joined: May 5th, 2007, 7:24 pm
Posts: 1240
Location: Seville, Spain
Using recursion would make the function crash on big numbers.
But it could be an option, too.

_________________
fincs
Highly recommended: AutoHotkey_L (see why) (all my code snippets require it)
Formal request to polyethene - I support the unity of the AutoHotkey community
Get SciTE4AutoHotkey v3.0.00 (Release Candidate)
[My project list]


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2009, 10:38 pm 
Offline

Joined: December 17th, 2007, 6:39 pm
Posts: 235
Location: Galati, Romania
Nice improvements, but the truth is my functions are easiest to understand for a novice. :D


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2009, 10:41 pm 
Offline
User avatar

Joined: May 5th, 2007, 7:24 pm
Posts: 1240
Location: Seville, Spain
Petru wrote:
Nice improvements, but the truth is my functions are easiest to understand for a novice. :D

Yeah... But you can always learn... Maybe I will tear here both functions into pieces, so that newbies can understand them better... ;)

_________________
fincs
Highly recommended: AutoHotkey_L (see why) (all my code snippets require it)
Formal request to polyethene - I support the unity of the AutoHotkey community
Get SciTE4AutoHotkey v3.0.00 (Release Candidate)
[My project list]


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2009, 10:52 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
fincs wrote:
Using recursion would make the function crash on big numbers.
Not if the numbers can be represented as 64 bit integers. The deepest recursion is 64 at large binary numbers, which is small enough for not crashing a normal PC.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2009, 10:53 pm 
Offline

Joined: December 17th, 2007, 6:39 pm
Posts: 235
Location: Galati, Romania
My informatics teacher always tells me to code the easy way, not the sophisticated way. This is how I got the first place at the informatics olympiad in my country. http://olimpiada.info/oji2009/index.php ... im&clasa=7 :D Now I'm going to the national contest and if I pass I go further to the international olympiad.... Aahh these are just dreams but it would be so could if I'd manage to do well there also...

In fact I use Autohotkey to understand some algorithms and translate them since there are no "for" or "repeat...until" or "do...while" cycles.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2009, 11:13 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Petru wrote:
My informatics teacher always tells me to code the easy way, not the sophisticated way.
For me recursion is easier than loops. The code is so much simpler, that makes debugging also easier.

If we keep your original restriction, the function becomes really short and easy to understand:
Code:
ToBase0(n,b) { ; n >= 0, 1 < b <= 10
   Return n < b ? n : ToBase0(n//b,b) . mod(n,b)
}

Petru wrote:
I use Autohotkey to understand some algorithms and translate them since there are no "for" or "repeat...until" or "do...while" cycles.
These are trivial to program, especially with the help of the new while-loop in AHK.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: maul.esel and 9 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group