- Home
·
- Math/Dates
·
- Convert from any nuumeric base to any other numeric base. Even works with decimals!
Convert from any nuumeric base to any other numeric base. Even works with decimals!
Convert from any nuumeric base to any other numeric base. Even works with decimals!
API Declarations
'num2$ = convertbase(num$,base1,base2)
Rate Convert from any nuumeric base to any other numeric base. Even works with decimals!
(2(2 Vote))
'This code created by CalFraud.
'Not to be modified (like I'm gonna stop anybody)
'Created for use with "Advance" Complex Graphing Calculator
'Extract value (convert to decimal)
Dim worth As Double
On Error GoTo handler
num$ = UCase$(num$)
If Not base1% = 10 Then
If InStr(num$, ".") > 0 Then decpos% = InStr(num$, "."): decpnt% = (Len(num$) + 1) - InStr(num$, "."): decim = True Else decpos% = 1: decpnt% = 1
worth = base1% / (base1% ^ decpnt%)
num$ = UCase$(num$)
For X% = Len(num$) To 1 Step -1
If decim = True Then If X% = decpos% Then X% = X% - 1: If X% < 1 Then GoTo done
dig$ = Mid(num$, X%, 1)
If Asc(dig$) > 64 Then digit% = Asc(dig$) - 55 Else digit% = Val(dig$)
newnum = newnum + digit% * worth
worth = worth * base1%
Next
Else
newnum = Val(num$)
End If
done:
If Not base2% = 10 Then
'Convert to base n
digit% = 0
While base2% ^ digit% <= newnum: digit% = digit% + 1: Wend
digit% = digit% - 1
While newnum > 0 Or digit% > -1
digval% = Int(newnum / (base2% ^ digit%))
'Use internal (faster) technique: <-- Any miscalculations here will be your processor's fault.
newnum = newnum - ((base2% ^ digit%) * digval%)
If digit% = -1 Then a$ = a$ + "."
If digval% < 10 Then a$ = a$ + Chr(48 + digval%) Else a$ = a$ + Chr(55 + digval%)
digit% = digit% - 1
Wend
Else: a$ = CStr(newnum)
End If
GoTo ok
handler:
'You can insert any error-handling procedures here.
'Quick error-code reference:
'5=illegal function call, 6 = Overflow, 11 = divide by zero
'
'Select Case Err
' Case Else
'End Select
Resume ok
ok:
convertbase$ = a$
End Function
Convert from any nuumeric base to any other numeric base. Even works with decimals! Comments
No comments yet — be the first to post one!
Post a Comment