b64dec.bac

REM
REM Demo implementing BASE64 decoder
REM
REM March 2009 - PvE.
REM Revised November 2009.
REM Revised May 2010.
REM Revised November 2013.
REM

REM Get the separate arguments
SPLIT ARGUMENT$ BY " " TO arg$ SIZE dim

IF dim < 2 THEN
    PRINT "Usage: b64enc <string>"
    END
ENDIF

REM Get the string to convert
dat$ = CHOP$(MID$(ARGUMENT$, INSTR(ARGUMENT$, " ")))

REM the LET statement is optional
CONST BASE64$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

WHILE LEN(dat$) > 0 DO

    REM Specify byte values of incoming string
    base1$ = MID$(dat$, 1, 1)
    base2$ = MID$(dat$, 2, 1)
    base3$ = MID$(dat$, 3, 1)
    base4$ = MID$(dat$, 4, 1)

    REM Now find the indexnumber
    byte1 = INSTR(BASE64$, base1$) - 1
    IF byte1 < 0 THEN byte1 = 0

    byte2 = INSTR(BASE64$, base2$) - 1
    IF byte2 < 0 THEN byte2 = 0

    byte3 = INSTR(BASE64$, base3$) - 1
    IF byte3 < 0 THEN byte3 = 0

    byte4 = INSTR(BASE64$, base4$) - 1
    IF byte4 < 0 THEN byte4 = 0

    REM Recalculate to ASCII values
    decoded$ = decoded$ & CHR$((byte1 & 63) * 4 + (byte2 & 48) / 16 )
    decoded$ = decoded$ & CHR$((byte2 & 15) * 16 + (byte3 & 60) / 4 )
    decoded$ = decoded$ & CHR$((byte3 & 3) * 64 + byte4 )

    REM Decrease incoming string with 4
    dat$ = RIGHT$(dat$, LEN(dat$) - 4)

WEND

REM Print result
PRINT decoded$

Generated by GNU Enscript 1.6.5.90.