76 lines
1.8 KiB
Plaintext
76 lines
1.8 KiB
Plaintext
#define DIGITS "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
|
|
Dim cadena(63) As String
|
|
|
|
Function mod2(a As Long, b As Integer) As Long
|
|
Return a - (a \ b) * b
|
|
End Function
|
|
|
|
Function StrReverse(Byval text As String) As String
|
|
Dim As String text2 = text
|
|
Dim As Integer x, lt = Len(text)
|
|
|
|
For x = 0 To lt Shr 1 - 1
|
|
Swap text2[x], text2[lt - x - 1]
|
|
Next x
|
|
Return text2
|
|
End Function
|
|
|
|
Function EncodeNegativeBase(Byval n As Long, base_ As Integer) As String
|
|
Dim As Long Puntero, idx, rem_
|
|
Dim result As String
|
|
|
|
If base_ > -1 Or base_ < -62 Then
|
|
Return result
|
|
Else
|
|
If n = 0 Then
|
|
Return "0"
|
|
Else
|
|
Puntero = 0
|
|
Do While n <> 0
|
|
rem_ = mod2(n, base_)
|
|
n \= base_
|
|
If rem_ < 0 Then
|
|
n += 1
|
|
rem_ = rem_ - base_
|
|
End If
|
|
result &= Mid(DIGITS, rem_ + 1, 1)
|
|
Loop
|
|
End If
|
|
End If
|
|
Return StrReverse(result)
|
|
End Function
|
|
|
|
Function DecodeNegativeBase(ns As String, base_ As Integer) As Long
|
|
Dim As Long total, bb
|
|
Dim As Integer i, j
|
|
|
|
If base_ < -62 Or base_ > -1 Then Return 0
|
|
If Mid(ns, 1, 1) = "0" Or (Mid(ns, 1, 1) = "0" And Mid(ns, 2, 1) = "0") Then Return 0
|
|
i = Len(ns)
|
|
total = 0
|
|
bb = 1
|
|
Do While i >= 1
|
|
j = Instr(DIGITS, Mid(ns, i, 1)) - 1
|
|
total += j * bb
|
|
bb *= base_
|
|
i -= 1
|
|
Loop
|
|
Return total
|
|
End Function
|
|
|
|
Sub Driver(n As Long, b As Integer)
|
|
Dim As String ns = EncodeNegativeBase(n, b)
|
|
Print Str(n); " encoded in base "; b; " = "; ns
|
|
Dim As Long p = DecodeNegativeBase(ns, b)
|
|
Print ns; " decoded in base "; b; " ="; p
|
|
Print
|
|
End Sub
|
|
|
|
Driver 10, -2
|
|
Driver 146, -3
|
|
Driver 15, -10
|
|
Driver 118492, -62
|
|
|
|
Sleep
|