Skip to content

Instantly share code, notes, and snippets.

@iversc
Last active May 23, 2019 04:49
Show Gist options
  • Save iversc/1487daf969b7c7c9de092b499911a691 to your computer and use it in GitHub Desktop.
Save iversc/1487daf969b7c7c9de092b499911a691 to your computer and use it in GitHub Desktop.
LB code for converting to/from Base85.
str$ = Base85Encode$("testing")
print str$
print Base85Decode$(str$)
print
str$ = Z85Encode$("testing")
print str$
print Z85Decode$(str$)
Function Base85Encode$(dat$)
ascii85$ = "!" + chr$(34) + "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstu"
Base85Encode$ = B85EncodeWithCode$(dat$, ascii85$)
End Function
Function Base85Decode$(dat$)
ascii85$ = "!" + chr$(34) + "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstu"
Base85Decode$ = B85DecodeWithCode$(dat$, ascii85$)
End Function
Function Z85Encode$(dat$)
Z85$ = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#"
Z85Encode$ = B85EncodeWithCode$(dat$, Z85$)
End Function
Function Z85Decode$(dat$)
Z85$ = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#"
Z85Decode$ = B85DecodeWithCode$(dat$, Z85$)
End Function
Function B85EncodeWithCode$(dat$, encoding$)
datLenMod = len(dat$) mod 4
if datLenMod then padding = 4 - datLenMod
for x = 1 to padding
dat$ = dat$ + chr$(0)
next x
For x = 1 to len(dat$) step 4
temp$ = mid$(dat$, x, 4)
For y = 1 to 4
conv = (conv * 256) + asc(mid$(temp$, y, 1))
Next y
while conv > 0
char = (conv mod 85) + 1
chunk$ = mid$(encoding$, char, 1) + chunk$
conv = int(conv / 85)
wend
B85EncodeWithCode$ = B85EncodeWithCode$ + chunk$
chunk$ = ""
Next x
B85EncodeWithCode$ = left$(B85EncodeWithCode$, len(B85EncodeWithCode$) - padding)
End Function
Function B85DecodeWithCode$(dat$, encoding$)
padChr$ = right$(encoding$, 1)
datLenMod = len(dat$) mod 5
if datLenMod then padding = 5 - datLenMod
For x = 1 to padding
dat$ = dat$ + padChr$
next x
For x = 1 to len(dat$) step 5
temp$ = mid$(dat$, x, 5)
For y = 1 to 5
t$ = mid$(temp$, y, 1)
conv = (conv * 85) + (instr(encoding$, t$) - 1)
Next y
while conv > 0
char = conv mod 256
chunk$ = chr$(char) + chunk$
conv = int(conv / 256)
wend
B85DecodeWithCode$ = B85DecodeWithCode$ + chunk$
chunk$ = ""
Next x
B85DecodeWithCode$ = left$(B85DecodeWithCode$, len(B85DecodeWithCode$) - padding)
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment