Attribute VB_Name = "B64_Lv64"
Option Explicit

' Example ASCII Number to Lv64 Number Encoder: txt_output.Text = VL64Encode(txt_input.Text)
Public Function VL64Encode(Data As String) As String
On Error Resume Next

Dim i As Long
Dim cod1 As String
Dim cod2 As String

If Data - 3 > 0 Then
  For i = 0 To 3
    If ((Data - i) Mod 4) = 0 Then
    cod1 = Chr(i + 80)
    cod2 = Chr((Data - i) / 4 + 64)
    End If
   Next i
VL64Encode = cod1 & cod2
Else
VL64Encode = Chr(Data + 72)
End If

' K = 45 Mistake
'If Data <= 183 Then
'VL64Encode = VL64Encode & " or " & Chr(Data + 72)
'Exit Function
'End If

End Function

' Example ASCII String to Lv64 Number Encoder: txt_output.Text = VL64Encode_2(txt_input.Text)
Public Function VL64Encode_2(Data As String) As String
On Error Resume Next

Dim i As Long
Dim cod1 As String
Dim cod2 As String

Data = Len(Data)

If Data - 3 > 0 Then
  For i = 0 To 3
    If ((Data - i) Mod 4) = 0 Then
    cod1 = Chr(i + 80)
    cod2 = Chr((Data - i) / 4 + 64)
    End If
   Next i
VL64Encode_2 = cod1 & cod2
Else
VL64Encode_2 = Chr(Data + 72)
End If

End Function

' Example Lv64 Number to ASCII Number Decoder: txt_output.Text = VL64Decode(txt_input.Text)
Public Function VL64Decode(Data As String) As String
On Error Resume Next

Dim Second As String
Dim nf As Long
Dim i As Long
Dim R As Long
Dim Id As String
Dim rawid As String

' K = 45 Mistake
If Len(Data) = 1 And Data <> "H" And Data <> "I" And Data <> "J" Then
Id = TextToASCII(Data) - 72
VL64Decode = Id
Exit Function
End If

nf = (Asc(Left(Data, 1)) - 72) / 4
If nf < 1 Then
nf = 0
End If
If (nf Mod 2) = 0 Then
   nf = nf / 2
   For i = 1 To nf
     R = R + ((Asc(Mid(Data, i + 1, 1)) - 64) * (64 ^ (i - 1)))
   Next i
   
Id = (4 * R) + ((Asc(Left(Data, 1)) - 72) Mod 4)
Else
rawid = Left(Data, 2)
Second = Replace(rawid, "S", "", 1, 1)
Id = (Asc(Second) - 64) * 4 + nf
End If

VL64Decode = Id

End Function

' Example ASCII Number to B64 Number Encoder: txt_output.Text = encodeB64(txt_input.Text)
Public Function encodeB64(Char As String) As String
On Error Resume Next

Dim i As Long
i = Len(Char)
encodeB64 = Chr(Fix((i / 64) + 64)) + Chr((i Mod 64) + 64)

End Function

' Example ASCII String to B64 Number Encoder: txt_output.Text = encodeB64_2(txt_input.Text)
Public Function encodeB64_2(Char As Long) As String
On Error Resume Next

encodeB64_2 = Chr(Fix((Char / 64) + 64)) + Chr((Char Mod 64) + 64)

End Function

' Example B64 Number to ASCII Number Decoder: txt_output.Text = decodeB64(txt_input.Text)
Public Function decodeB64(Char As String) As Long
On Error Resume Next

Dim char1 As Long
Dim char2 As Long
    char1 = (Asc(Left$(Char, 1)) - 64) * 64
    char2 = Asc(Right$(Char, 1)) - 64
    decodeB64 = char1 + char2



End Function

' Example ASCII String to ASCII Number Encoder: txt_output.Text = Len(txt_input.Text)
' Function is a Visual Basic Main Function

' Text String to ASCII String
Public Function TextToASCII(Str As String) As String
On Error Resume Next

Dim strLen As Long
strLen = Len(Str)
Dim tmpChar As Integer
Dim tmpBuffer As String
Dim i As Long

Dim Number As String

For i = 1 To strLen
    tmpChar = Asc(Mid(Str, i, 1))
    tmpBuffer = tmpBuffer & tmpChar
    If Len(Str) > 1 And i < strLen Then
    tmpBuffer = tmpBuffer & " "
    End If
Next
TextToASCII = tmpBuffer

End Function

' ASCII String to Text String
Public Function ASCIIToText(Str As String) As String
On Error Resume Next

Dim HexCount As Long
HexCount = StrCount(Str, " ")
Dim tmpHex As String
Dim tmpHex2 As Long
Dim tmpBuffer As String
Dim i As Long
For i = 1 To HexCount + 1
    If Split(Str, " ")(i - 1) <> Empty Then
    tmpHex = Split(Str, " ")(i - 1)
    tmpHex2 = tmpHex
    tmpBuffer = tmpBuffer & Chr(tmpHex2)
    End If
Next
ASCIIToText = tmpBuffer
End Function

