TNO
-
Total Posts
:
2094
- Scores: 36
-
Reward points
:
0
- Joined: 12/18/2004
- Location: Earth
-
Status: offline
|
Base Conversion
Tuesday, October 24, 2006 10:32 PM
( permalink)
Heres a function to convert a number into any other number between base 2-36 (decimal to anything between binary and trientehexanary [I made that up  ]) Special thanks to ebgreen for assisting with this. VBScript:
'Function BaseCon(N,radix)
' return representation of a number N
' in the system based on radix
Function BaseCon(N, radix)
Dim hex
hex = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim s
s = ""
Dim A
A = N
While A >= radix
B = A Mod radix
A = Int(A / radix)
s = s & Mid(hex,B+1,1)
Wend
s = s & Mid(hex,A+1,1)
BaseCon = StrReverse(s)
End Function
MsgBox BaseCon(15,16) JScript:
// function DecimaltoAnother(N, radix)
//
// return representation of a number N
// in the system based on radix
//
function DecimaltoAnother(N, radix)
{
hex = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
s = "";
A = N;
while (A >= radix)
{
B = A % radix;
A = Math.floor(A / radix);
s += hex.charAt(B);
}
s += hex.charAt(A);
return Transpose(s);
}
// function Transpose(s)
//
// return a string written from right to left
//
function Transpose(s)
{
N = s.length;
t = "";
for (i = 0; i < N; i++)
t = t + s.substring(N-i-1, N-i);
s = t;
return s;
}
WScript.Echo(DecimaltoAnother(99,36));
To iterate is human, to recurse divine. -- L. Peter Deutsch
|
|
|
|
XiaoAiWhc
-
Total Posts
:
6
- Scores: 0
-
Reward points
:
0
- Joined: 11/10/2005
-
Status: offline
|
RE: Base Conversion
Thursday, October 26, 2006 10:35 PM
( permalink)
This is a very nice script!!
|
|
|
|