|
Joined: Nov 2003
Posts: 2,327
Hoopy frood
|
OP
Hoopy frood
Joined: Nov 2003
Posts: 2,327 |
Could anybody explain the base algorithm or point me to somewhere that does please? I tried google and i didn't find anything other than one place that i didn't understand.
New username: hixxy
|
|
|
|
Joined: Jan 2003
Posts: 2,523
Hoopy frood
|
Hoopy frood
Joined: Jan 2003
Posts: 2,523 |
What do you mean by "base"? Are you talking about the base conversion algorithm $base() uses?
/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
|
|
|
|
Joined: Nov 2003
Posts: 2,327
Hoopy frood
|
OP
Hoopy frood
Joined: Nov 2003
Posts: 2,327 |
New username: hixxy
|
|
|
|
Joined: Jan 2003
Posts: 2,523
Hoopy frood
|
Hoopy frood
Joined: Jan 2003
Posts: 2,523 |
I don't know which algorithm mirc uses (there are a few of them), but you can find plenty of results if you search Google for "base conversion algorithm" (including the quotes). The first result I found is this, which analyzes two algorithms and explains them rather well, although it doesn't take care of cases where the base is greater than 10. I've written a custom identifier equivalent to $base (minus the zeropad and precision features), but I doubt I could explain its function better than those websites explain the function of their examples. I didn't even comment it (almost), as I doubt it would help. If you are good at maths and you understand the recursive algorithm explained on the above page, you'll probably figure out how it works. [color:green]/*
$mybase() converts a number ($1) from any base ($2) to base-10
It then passes the resulting decimal number and the outbase ($3) to $mybase2()
Its usage is the same as $base(), except that it doesn't
support [zeropad], [precision] or non-integers.
*/[/color]
alias mybase {
if $2 != 10 {
var %i = 1, %a = 0
while $mid($1,- $+ %i,1) != $null {
inc %a $calc($2 ^ (%i - 1) * ($pos(0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ,$ifmatch) - 1))
inc %i
}
return $mybase2(%a,$3)
}
return $mybase2($1,$3)
}
[color:green]; $mybase2() converts a number ($1) from base-10 to the specified base ($2).
; It implements the recursive algorithm explained in the site I linked above[/color]
alias mybase2 {
if $1 < $2 { return $mid(0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ,$calc($1 + 1),1) }
return $mybase3($int($calc($1 / $2)),$2) $+ $mid(0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ,$calc($1 % $2 + 1),1)
}
[color:green]; $mybase3() is just there to make recursion possible in mIRC[/color]
alias mybase3 return $mybase2($1,$2)
/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
|
|
|
|
Joined: Nov 2003
Posts: 2,327
Hoopy frood
|
OP
Hoopy frood
Joined: Nov 2003
Posts: 2,327 |
yeah, that was the site i didn't understand, thanks anyway.
New username: hixxy
|
|
|
|
Joined: Aug 2003
Posts: 72
Babel fish
|
Babel fish
Joined: Aug 2003
Posts: 72 |
Here is the code i wrote for a challenge:
alias base2 {
var %a 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ, %i -1, %t, %r
while $mid($1,%i,1) >= 0 {
inc %t $calc(($pos(%a,$ifmatch)-1)*$2 ^-(%i +1))
dec %i
}
while $int(%t) >=0 {
%r = $mid(%a,$calc(%t % $3 +1),1) $+ %r
%t = %t / $3
}
return %r
}
It starts by converting the given number in base 10, then convert the base 10 number in given base To convert hexadecimal (base16) in decimal (base 10): 29A = 2 9 10 29A = 2*(16^2) + 9*(16^1) + 10*(16^0) 29A = 666 To convert binary (base2) in decimal(base10): 10010 = 1 0 0 1 0 10010 = 1*(2^4) + 0*(2^3) + 0*(2^2) + 1*(2^1) + 0*(2^0) Then, if you want to convert a base 10 into a base 16: echo -a $base(666,10,16) = 29A int(666 / (16 ^ 0)) % 16 = 10 = A int(666 / (16 ^ 1)) % 16 = 9 = 9 int(666 / (16 ^ 2)) % 16 = 2 = 2 Then you read the number backwards... tada!! 29A With that, you should be able to understand how the snippet work!
|
|
|
|
Joined: Jan 2003
Posts: 2,523
Hoopy frood
|
Hoopy frood
Joined: Jan 2003
Posts: 2,523 |
I see our methods are essentially the same, except that yours fits nicely into one alias, since it uses iteration and not recursion Just so you know, there's a small typo in your code, this line: while $int(%t) >=0 should be: while $int(%t) > 0 or (faster): while %t >= 1
Last edited by qwerty; 08/05/04 11:51 AM.
/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
|
|
|
|
Joined: Feb 2003
Posts: 2,812
Hoopy frood
|
Hoopy frood
Joined: Feb 2003
Posts: 2,812 |
(to both): is a two step conversion necessary? Think it would be possible to convert from one base to the next in a single step?
Well. At least I won lunch. Good philosophy, see good in bad, I like!
|
|
|
|
Joined: Dec 2003
Posts: 61
Babel fish
|
Babel fish
Joined: Dec 2003
Posts: 61 |
For the ones who has a TI-83 calculator:
PROGRAM:BASE
:ClrHome
:Input "VALUE:",Str1
:Input "INBASE:",B
:Input "OUTBASE:",C
:"1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" ==> Str2
:DelVar Z
:length(Str1) ==> E
:For(A,1,E)
:Z+instring(Str2,sub(Str1,A,1))*B^(E-A) ==> Z
:End
:" " ==> Str3
:For(A,round(log(Z)/log(C),0),0,-1)
:int(Z/C^A) ==> D
:Z-D*C^A ==> Z
:If D=0 and Str3!=" "
:Str3+"0" ==> Str3
:If D!=0
:Str3+sub(Str2,D,1) ==> Str3
:End
:Disp "RESULT:"
:sub(Str3,3,length(Str3)-1)
|
|
|
|
|