VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Convert from any nuumeric base to any other numeric base. Even works with decimals!

by CalFraud (1 Submission)
Category: Math/Dates
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Mon 2nd December 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)

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!



'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


Download this snippet    Add to My Saved Code

Convert from any nuumeric base to any other numeric base. Even works with decimals! Comments

No comments have been posted about Convert from any nuumeric base to any other numeric base. Even works with decimals!. Why not be the first to post a comment about Convert from any nuumeric base to any other numeric base. Even works with decimals!.

Post your comment

Subject:
Message:
0/1000 characters