Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working on Base-85 Encoder/Decoder Functions
#8
Steve, the Base-128 is the most space efficient yet.  Will be great for stuffing files in code.  A 256 byte input will output 293 byte encoded. My Base85 outputs 321 bytes for the the same input, larger.  However, I seem to be getting an extra byte added converting 128 back to 256.

Here's what I'm testing with...

- Dav

Code: (Select All)

_ControlChr Off
For i = 0 To 255
    a1$ = a1$ + Chr$(i)
Next

Color 7: Print a1$
Color 11: Print Len(a1$); "bytes"
Print
a2$ = B256to128(a1$)

Color 7: Print a2$
Color 11: Print Len(a2$); "bytes"
Print
a3$ = B128to256(a2$) 'adding an extra byte at end?

Color 7: Print a3$
Color 11: Print Len(a3$); "bytes"


Function B256to128$ (text$)
    l = 8 * Len(text$)
    Dim A(1 To l) As _Unsigned _Bit, b As _Unsigned _Byte
    'convert the text to the 8 bit array
    For i = 1 To Len(text$)
        b = Asc(text$, i)
        p = (i - 1) * 8 + 1
        For j = 0 To 7
            If b And (2 ^ j) Then A(p + j) = 1 Else A(p + j) = 0
        Next
    Next
    'convert the array to 7bit strings
    For i = 1 To l Step 7
        b = 0
        For j = 6 To 0 Step -1
            If i + j < l Then
                If A(i + j) Then b = b + (2 ^ j)
            End If
        Next
        b = b + 45
        t$ = t$ + Chr$(b)
    Next
    If Len(t$) Mod 8 <> 0 And Right$(t$, 1) = "-" Then t$ = Left$(t$, Len(t$) - 1)
    B256to128 = t$
End Function

Function B128to256$ (text$)
    l = 7 * Len(text$)
    Dim A(1 To l) As _Unsigned _Bit, b As _Unsigned _Byte
    'convert the text to the 8 bit array
    For i = 1 To Len(text$)
        b = Asc(text$, i) - 45
        For j = 0 To 6
            p = p + 1: If p > l Then Exit For
            If b And (2 ^ j) Then A(p) = 1 Else A(p) = 0
        Next
    Next
    'convert the array to 8bit strings
    p = 0
    For i = 1 To l Step 8
        b = 0
        For j = 0 To 7
            p = p + 1
            If p > l Then Exit For
            If A(p) Then b = b + (2 ^ j)
        Next
        t$ = t$ + Chr$(b)
    Next
    B128to256 = t$
End Function

Find my programs here in Dav's QB64 Corner
Reply


Messages In This Thread
RE: Working on Base-85 Encoder/Decoder Functions - by Dav - 09-23-2023, 01:18 PM



Users browsing this thread: 7 Guest(s)