AutoHotkey Community

It is currently May 26th, 2012, 6:34 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: March 9th, 2007, 6:38 pm 
In desperate need of a fast way to convert a large base 10 number into base 36.

Have searched the forum and found all other conversions, but not base 36, can anyone assist??????


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 9th, 2007, 7:26 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Code:
;;;;; ----- Convert number to any base <= 36 (octal, hex...)
ToBase(n,b) { ; n >= 0, 1 < b <= 36
   Loop {
      d := mod(n,b), n //= b
      m := (d < 10 ? d : Chr(d+55)) . m
      IfLess n,1, Break
   }
   Return m
}

MsgBox % ToBase(73,36)
MsgBox % ToBase(36*36+35,36)
MsgBox % ToBase(9,8)
MsgBox % ToBase(66,8)
MsgBox % ToBase(100,16)
MsgBox % ToBase(255,16)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 9th, 2007, 7:53 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
...or a single line recursive function
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))
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 9th, 2007, 8:38 pm 
Thanks worked like a charm, you are indeed a genius.

Thanks again. :lol: :D


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 9th, 2007, 8:50 pm 
Offline

Joined: February 13th, 2006, 10:40 pm
Posts: 389
Location: Utah
Superfraggle79 wrote:
you are indeed a genius.

Amen!

_________________
Image
"Power can be given overnight, but responsibility must be taught. Long years go into its making."


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 4th, 2008, 5:25 am 
Offline

Joined: June 21st, 2007, 10:10 pm
Posts: 41
Location: 43.074233,-72.424707
it stops working when passed a float. i suggest:

ToBase(n,b)
{
; n >= 0, 1 < b <= 36
if n is not integer
return "error"
Return (n < b ? "" : ToBase(n//b,b)) . ((d:=mod(n,b)) < 10 ? d : Chr(d+55))
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 4th, 2008, 7:02 am 
Offline

Joined: January 2nd, 2008, 4:47 am
Posts: 150
Location: Freenode IRC
Nice function, as always Laszlo!

Do recursive functions work efficiently (faster than loop?) and safely (stack overflow?) in AutoHotKey?

I know that C and other languages will try to optimize recursive functions, such as with tail recursion, but does AHK?

Also, when you say octal, do you mean decimal or truly octal (base 8) values?

_________________
Image

Need help right away? Get live support on IRC.
Already have an IRC client installed? /join #ahk


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 4th, 2008, 7:25 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
apocalypse~r wrote:
it stops working when passed a float.
These functions demonstrate the algorithms. If you want to put them in a library, of course, some error checking is needed.
Raccoon wrote:
Do recursive functions work efficiently (faster than loop?) and safely (stack overflow?) in AutoHotKey?
Loops are faster, but these functions are not meant to be used in time critical applications, but to convert a few numbers, like in a calculator. If you need speed, you have to use machine code functions, instead.

AHK allows hundreds of levels of recursion with its standard settings. In our case, each call results in a new digit, so there will never be more than 64 levels of the recursion, therefore it is safe.
Raccoon wrote:
when you say octal, do you mean decimal or truly octal (base 8) values?
Base 8.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 4th, 2008, 8:23 am 
Offline

Joined: January 2nd, 2008, 4:47 am
Posts: 150
Location: Freenode IRC
BTW, sorry about that last question. I misread that comment as meaning acceptable inputs were hex and oct for some reason. :S

Thanks, I'll have to add this to my \Libs. Source citation, yourself?

Edit: Would you mind expanding on this, to allow for in-base and out-base? ie, converting base36 back to base10. My method depends on converting to a neutral common base (base 10) before converting to another. eg: base(xyzzy, 36, 16) would convert xyzzy from base36 to base10 to base16. I'm certain you could cut out the middle-man. :)

_________________
Image

Need help right away? Get live support on IRC.
Already have an IRC client installed? /join #ahk


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 4th, 2008, 4:38 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
As I said, the above functions were meant to demonstrate the algorithms. The base conversion functions are built into the C run time library supplied with Windows. You only need a wrapper:
Code:
MsgBox % B2B("10101010",2,10) ; binary to decimal (170)
MsgBox % B2B("1234",5,16)     ; base 5 to hex (c2)

B2B(NumStr,InputBase,OutputBase) { ; Convert between number represetations in any base (radix)
  VarSetCapacity(S,65,0)           ; signed input OK; output unsigned, except for OutputBase = 10
  num := DllCall("msvcrt\_strtoui64", "Str",NumStr, "Uint",0, "UInt",InputBase, "CDECL Int64")
  DllCall("msvcrt\_i64toa", "Int64",num, "Str",S, "UInt",OutputBase, "CDECL")
  Return S
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 4th, 2008, 11:37 pm 
Offline

Joined: December 4th, 2006, 10:35 am
Posts: 561
Location: Galil, Israel
@Laszlo,

cool stuff. (Amazing how ignorant I feel sometimes when looking at your stuff)...

_________________
Joyce Jamce


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 5th, 2008, 1:04 am 
I must say, Lazlo, you owned your first post with your second one's code. lol


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 5th, 2008, 1:38 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Thanks, guys, but I use the third function (the wrapper for msvcrt) in my popup calculator. It is the fastest and small, although has no educational value.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 6th, 2008, 10:30 am 
So now you must educate me on how to write a base algorithm that converts baseA to baseB without stopping at base10 in between. :) No nested loops, no recursion, just math.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 6th, 2008, 4:34 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
The wrapper version stops at binary in between, not base 10. Most computers do internal arithmetic in only a few bases (binary, BCD…), so you need to stop there, unless you want to emulate arbitrary base arithmetic. It would be complicated and slow, so why do you want it? To learn how long division works?


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: bobbysoon, Google [Bot], JSLover, kkkddd1, Tipsy3000, Yahoo [Bot] and 69 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