Quick feature description:
The program encodes a text string into a binary string that is encoded in five bits. To achieve this, thanks to the MEM functions, I manipulate a variable of type _Integer64, where I fill the first 5 bytes with five bits, thereby placing 8 characters of the text string in these 5 bytes. Subsequently, I save the variable of type Integer64 in memory and in the next step I overwrite its last three bytes. This gives me a five-byte value that is not directly available in QB64. Finally, I extract the valid length from the memory to the string (for 8 characters it is 40 bits, so 5 bytes).
During decoding, the coded string is inserted into memory and is read from it using a string of five bytes, and thanks to the MEM options, this variable is inserted into a number of type Integer64. This number is then decomposed back into the individual array indices that determine the specific letters of the encoded string.
I have completely avoided the previous Dec2Bin and Bin2Dec practices here that you can see in my old programs.
While this whole thing was supposed to be a nice distraction and fun, it turned into a huge embarrassment. I really don't know what I neglected, that's why I want to ask you for help.
Now if you run this program as is, it will work correctly. But. Please disable MemFill on line 81 and run it again.
Where do the artifacts come from? Where does the new memory array take those bogus values? I am not able to deduce it logically and I do not see it in the program (with my little knowledge of boolean operations). Can someone explain this to me?
The program encodes a text string into a binary string that is encoded in five bits. To achieve this, thanks to the MEM functions, I manipulate a variable of type _Integer64, where I fill the first 5 bytes with five bits, thereby placing 8 characters of the text string in these 5 bytes. Subsequently, I save the variable of type Integer64 in memory and in the next step I overwrite its last three bytes. This gives me a five-byte value that is not directly available in QB64. Finally, I extract the valid length from the memory to the string (for 8 characters it is 40 bits, so 5 bytes).
During decoding, the coded string is inserted into memory and is read from it using a string of five bytes, and thanks to the MEM options, this variable is inserted into a number of type Integer64. This number is then decomposed back into the individual array indices that determine the specific letters of the encoded string.
I have completely avoided the previous Dec2Bin and Bin2Dec practices here that you can see in my old programs.
While this whole thing was supposed to be a nice distraction and fun, it turned into a huge embarrassment. I really don't know what I neglected, that's why I want to ask you for help.
Now if you run this program as is, it will work correctly. But. Please disable MemFill on line 81 and run it again.
Where do the artifacts come from? Where does the new memory array take those bogus values? I am not able to deduce it logically and I do not see it in the program (with my little knowledge of boolean operations). Can someone explain this to me?
Code: (Select All)
'S2FB$ - String to five bite string
'FB2S$ - five bite string back to normal 8 bit string
'allowed characters: abcdefghijklmnopqrstuvwxyz+- '
'can be expanded, but this is not implanted in this version.
txt$ = "text to five byte binary text coder and decoder - first version"
'
t$ = S2FB$(txt$) 'string to five byte code (return binary string code)
Do
u$ = FB2S$(t$) 'decode binary string t$ to normal text u$
Print u$
t$ = S2FB$(txt$) 'make 5 bit coded string from normal text string txt$
_Limit 20
Loop
Function S2FB$ (S As String) 'returns a binary string encoded in the form of an index in the range 0 to 31, where each number is encoded in five bits according to the internal character table
Stable$ = "abcdefghijklmnopqrstuvwxyz+- '"
Dim InputTable(Len(S$)) As _Unsigned _Byte
i = 0
For test = 1 To Len(S$)
ch1$ = Mid$(S$, test, 1)
Allow = 0
For compare = 1 To Len(Stable$)
ch2$ = Mid$(Stable$, compare, 1)
If ch2$ = ch1$ Then
Allow = 1
InputTable(i) = compare 'set indexes to characters in S string
i = i + 1
End If
Next compare
If Allow = 0 Then Print "Can not create B5$. Unexpected character: "; ch1$: _Display: Sleep 3: End
Next test
Dim prd As _Unsigned _Integer64
Dim me As _MEM
If Len(S$) > 32 Then me = _MemNew(Len(S$)) Else me = _MemNew(32)
j = 0
meStep = 0
prd = 0
Do Until j > i
For t = 1 To 7
If j > UBound(InputTable) Then Exit For
prd = prd Or InputTable(j)
prd = _ShL(prd, 5)
j = j + 1
Next t
_MemPut me, me.OFFSET + meStep, prd
meStep = meStep + 5
prd = 0
Loop
prd = 0
Dim V As _Unsigned _Byte
V = 0
Code5$ = Space$(meStep)
_MemGet me, me.OFFSET, Code5$
S2FB$ = Code5$
_MemFree me
Erase InputTable
End Function
Function FB2S$ (code As String) ' Function decode binary string coded in 5 bite and return it as normal 8bit text.
Stable$ = "abcdefghijklmnopqrstuvwxyz+- '"
Dim me As _MEM
MSize = Len(code) * 1.6
If MSize < 16 Then MSize = 16
me = _MemNew(MSize)
_MemFill me, me.OFFSET, me.SIZE, 0 As _UNSIGNED _BYTE 'this is VERY VERY NEED HERE.
_MemPut me, me.OFFSET, code$
Dim hodnota As _Unsigned _Integer64
Dim Vse As _Offset
Dim Var As _MEM
Dim prd As _Unsigned _Integer64
prd = 0
Var = _Mem(prd)
If MSize < 5 Then swp$ = Space$(MSize) Else swp$ = Space$(5)
outb$ = ""
Vse = 0
Do Until Vse > me.SIZE - 5
uu = uu + 1
_MemGet me, me.OFFSET + Vse, swp$
_MemPut Var, Var.OFFSET, swp$
ReDim swp(1 To 8) As _Unsigned _Byte
For pr = 1 To 8
hodnota = prd And 31
swp(9 - pr) = hodnota
prd = _ShR(prd, 5)
Next
For dis = 1 To 8
outb$ = outb$ + Mid$(Stable$, swp(dis), 1)
Next
ReDim swp(1 To 8) As _Unsigned _Byte
Vse = Vse + 5
prd = 0
Loop
swp$ = ""
FB2S$ = outb$
_MemFree me
_MemFree Var
End Function