 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Superfraggle79 Guest
|
Posted: Fri Mar 09, 2007 6:38 pm Post subject: Base 10 to Base 36 Conversion |
|
|
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?????? |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 3959 Location: Pittsburgh
|
Posted: Fri Mar 09, 2007 7:26 pm Post subject: |
|
|
| 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) |
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 3959 Location: Pittsburgh
|
Posted: Fri Mar 09, 2007 7:53 pm Post subject: |
|
|
...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))
} |
|
|
| Back to top |
|
 |
Superfraggle79 Guest
|
Posted: Fri Mar 09, 2007 8:38 pm Post subject: |
|
|
Thanks worked like a charm, you are indeed a genius.
Thanks again.  |
|
| Back to top |
|
 |
Veovis
Joined: 13 Feb 2006 Posts: 390 Location: Utah
|
Posted: Fri Mar 09, 2007 8:50 pm Post subject: |
|
|
| Superfraggle79 wrote: | | you are indeed a genius. |
Amen! _________________
"Power can be given overnight, but responsibility must be taught. Long years go into its making." |
|
| Back to top |
|
 |
apocalypse~r
Joined: 21 Jun 2007 Posts: 19
|
Posted: Tue Mar 04, 2008 5:25 am Post subject: |
|
|
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))
} |
|
| Back to top |
|
 |
Raccoon
Joined: 02 Jan 2008 Posts: 70
|
Posted: Tue Mar 04, 2008 7:02 am Post subject: |
|
|
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 values? _________________ Need help right away? Get live support on IRC.
Already have an IRC client installed? /join #autohotkey |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 3959 Location: Pittsburgh
|
Posted: Tue Mar 04, 2008 7:25 am Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
Raccoon
Joined: 02 Jan 2008 Posts: 70
|
Posted: Tue Mar 04, 2008 8:23 am Post subject: |
|
|
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.  _________________ Need help right away? Get live support on IRC.
Already have an IRC client installed? /join #autohotkey |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 3959 Location: Pittsburgh
|
Posted: Tue Mar 04, 2008 4:38 pm Post subject: |
|
|
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
} |
|
|
| Back to top |
|
 |
Joy2DWorld
Joined: 04 Dec 2006 Posts: 418 Location: Galil, Israel
|
Posted: Tue Mar 04, 2008 11:37 pm Post subject: |
|
|
@Laszlo,
cool stuff. (Amazing how ignorant I feel sometimes when looking at your stuff)... _________________ Joyce Jamce |
|
| Back to top |
|
 |
Guest
|
Posted: Wed Mar 05, 2008 1:04 am Post subject: |
|
|
| I must say, Lazlo, you owned your first post with your second one's code. lol |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 3959 Location: Pittsburgh
|
Posted: Wed Mar 05, 2008 1:38 am Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
Guest
|
Posted: Thu Mar 06, 2008 10:30 am Post subject: |
|
|
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. |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 3959 Location: Pittsburgh
|
Posted: Thu Mar 06, 2008 4:34 pm Post subject: |
|
|
| 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? |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|