' B4X version of SJ64 encoding and decoding
Dim SJ64 As String = "E1rSo=2dKkjqmPTsLvhWZOQguHyxt9JD+z35FAUibfIw/NeBCaX4V78pGnYRMlc06" ' Qui puoi cambiare la mappa come preferisci
Public Sub SJ64enc(txt As String) As String
Dim SJ64stop As String = SJ64.SubString(SJ64.Length - 1)
Dim txtb() As Byte = txt.GetBytes("UTF8")
Dim sb As StringBuilder
sb.Initialize
For ptr = 0 To txtb.Length - 1 Step 3
Dim Vn As Int = 256 * txtb(ptr)
If ptr + 1 < txtb.Length Then
Vn = Vn + 256 * txtb(ptr + 1)
End If
If ptr + 2 < txtb.Length Then
Vn = Vn + txtb(ptr + 2)
End If
Dim Oct As String = NumberFormat(Vn, 8, 8).PadStart(8, "0"c)
sb.Append(SJ64.SubString(NumberFormat(Oct.SubString2(0, 2), 8), 1))
sb.Append(SJ64.SubString(NumberFormat(Oct.SubString2(2, 2), 8), 1))
sb.Append(SJ64.SubString(NumberFormat(Oct.SubString2(4, 2), 8), 1))
sb.Append(SJ64.SubString(NumberFormat(Oct.SubString2(6, 2), 8), 1))
Next
Select txt.Length Mod 3
Case 1 ' 8 bit finali
sb.Remove(sb.Length - 2, 2)
Case 2 ' 16 bit finali
sb.Remove(sb.Length - 1, 1)
End Select
Return sb.ToString
End Sub
Public Sub SJ64dec(txt As String) As String
Dim SJ64stop As String = SJ64.SubString(SJ64.Length - 1)
txt = txt.Replace(Chr(13), "").Replace(Chr(10), "").Replace(Chr(9), "").Replace(" "c, "")
Do Until txt.Length Mod 4 = 0
txt = txt & SJ64stop
Loop
Dim fp As OutputStream
fp.InitializeToBytesArray(0)
For ptr = 0 To txt.Length - 1 Step 4
Dim ris As String = ""
Dim numByte As Int = 3
For x = 0 To 3
Dim cc As String = txt.SubString2(ptr + x, ptr + x + 1)
If cc = SJ64stop Then
numByte = numByte - 1
ris = ris & "00"
Else
ris = ris & NumberFormat(SJ64.IndexOf(cc), 8).PadStart(2, "0"c)
End If
Next
Dim Vn As Int = NumberFormat(ris, 8)
fp.WriteBytes(Array As Byte((Bit.And(Vn, &HFF0000) / &H10000), (Bit.And(Vn, &HFF00) / &H100), (Bit.And(Vn, &HFF))))
Next
Dim risb() As Byte = fp.ToBytesArray
fp.Close
Return BytesToString(risb, 0, risb.Length, "UTF8")
End Sub