'Converts Strings to Numbers and back.
'(uses functions Fmod & Fix to allow use of Double data type)
' 10 x 26 characters:
' Use constants below for strings up to 10 characters in length using A to Z CAPS ONLY:
Const CharStart = "A"
Const CharStop = "Z"
' 6 x 63 characters:
' Use constants below for strings up to 7 characters in length using characters:
' ?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
'Const CharStart = "?"
'Const CharStop = "~"
Public CharMin As Byte, CharMax As Byte
Public expFactor As Double, chrOffset As Double
Sub SetRange()
expFactor = Asc(CharStop) - Asc(CharStart) + 2
chrOffset = Asc(CharStart) - 1
End Sub
Function FMod(a As Double, b As Double) As Double
'replaces MOD function so that it works with Double
'https://stackoverflow.com/questions/23549947/mod-with-doubles
FMod = a - Fix(a / b) * b
If FMod >= -2 ^ -52 And FMod <= 2 ^ -52 Then '+/- 2.22E-16
FMod = 0
End If
End Function
Function convert_String_to_Number(numIn As String) As Double
Dim ret As Double, x As Integer
SetRange
For x = 1 To Len(numIn)
If Asc(Mid(numIn, x, 1)) < Asc(CharStart) _
Or Asc(Mid(numIn, x, 1)) > Asc(CharStop) Then _
MsgBox "Invalid Character: " & Mid(numIn, x, 1)
ret = ret + ((Asc(Mid(numIn, x, 1)) - chrOffset) * expFactor ^ (Len(numIn) - x))
Next x
convert_String_to_Number = ret
End Function
Function convert_Number_to_String(decIn As Double) As String
Dim x As Integer, y As Integer
Dim numChars As Byte, d(25) As Double, ret As String
SetRange
numChars = (Int(Log(decIn) / Log(expFactor))) + 1
For x = numChars To 1 Step -1
d(x) = decIn
For y = 1 To numChars - x
d(x) = Fix(d(x) / expFactor)
Next y
d(x) = FMod(d(x), expFactor)
ret = Chr(chrOffset + d(x)) + ret
Next x
convert_Number_to_String = ret
End Function