Base Conversion

Author Message
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)
0
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
 
#1
    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)
    0
    This is a very nice script!!
     
    #2

      Online Bookmarks Sharing: Share/Bookmark

      Jump to:

      Current active users

      There are 0 members and 1 guests.

      Icon Legend and Permission

      • New Messages
      • No New Messages
      • Hot Topic w/ New Messages
      • Hot Topic w/o New Messages
      • Locked w/ New Messages
      • Locked w/o New Messages
      • Read Message
      • Post New Thread
      • Reply to message
      • Post New Poll
      • Submit Vote
      • Post reward post
      • Delete my own posts
      • Delete my own threads
      • Rate post

      2000-2012 ASPPlayground.NET Forum Version 3.9