' ASCII Table — prints printable ASCII characters in formatted table
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL idx AS LONG
    LOCAL ch AS STRING
    LOCAL hexVal AS STRING
    LOCAL decVal AS STRING
    LOCAL line AS STRING
    LOCAL pad AS STRING
    LOCAL row AS LONG
    LOCAL numRows AS LONG
    LOCAL cols AS LONG
    LOCAL cell AS STRING
    LOCAL cellWidth AS LONG
    LOCAL remaining AS LONG
    LOCAL spaces AS LONG
    LOCAL k AS LONG
    LOCAL startVal AS LONG
    LOCAL endVal AS LONG
    LOCAL section AS STRING
    PRINT "============================================"
    PRINT "          ASCII TABLE (32-126)"
    PRINT "============================================"
    PRINT ""
    numRows = 10
    cols = 10
    row = 0
    line = ""
    FOR i = 0 TO 9
        line = line + "+"
        FOR j = 0 TO 0
            line = line + "-------+"
        NEXT j
        FOR j = 0 TO 9
            line = line + "------+"
        NEXT j
    NEXT i
    PRINT line
    FOR i = 0 TO 9
        line = "|       |"
        FOR j = 0 TO 9
            idx = i * 10 + j + 32
            IF idx > 126 THEN
                line = line + "      |"
            ELSE
                ch = CHR$(idx)
                IF idx = 32 THEN
                    ch = "SP"
                END IF
                IF idx = 127 THEN
                    ch = "DEL"
                END IF
                spaces = 6 - LEN(ch)
                pad = ""
                FOR k = 1 TO spaces
                    pad = pad + " "
                NEXT k
                line = line + " " + ch + pad + "|"
            END IF
        NEXT j
        PRINT line
        line = "|       |"
        FOR j = 0 TO 9
            idx = i * 10 + j + 32
            IF idx > 126 THEN
                line = line + "      |"
            ELSE
                decVal = STR$(idx)
                spaces = 6 - LEN(decVal)
                pad = ""
                FOR k = 1 TO spaces
                    pad = pad + " "
                NEXT k
                line = line + " " + decVal + pad + "|"
            END IF
        NEXT j
        PRINT line
        line = "|       |"
        FOR j = 0 TO 9
            idx = i * 10 + j + 32
            IF idx > 126 THEN
                line = line + "      |"
            ELSE
                hexVal = HEX$(idx)
                IF LEN(hexVal) = 1 THEN
                    hexVal = "0" + hexVal
                END IF
                hexVal = "$" + hexVal
                spaces = 6 - LEN(hexVal)
                pad = ""
                FOR k = 1 TO spaces
                    pad = pad + " "
                NEXT k
                line = line + " " + hexVal + pad + "|"
            END IF
        NEXT j
        PRINT line
        line = "|       |"
        FOR j = 0 TO 9
            idx = i * 10 + j + 32
            IF idx > 126 THEN
                line = line + "      |"
            ELSE
                line = line + "------+"
            END IF
        NEXT j
        PRINT line
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "          DOUBLE ASCII (160-255)"
    PRINT "============================================"
    PRINT ""
    row = 0
    line = ""
    FOR i = 0 TO 9
        line = line + "+"
        FOR j = 0 TO 9
            line = line + "------+"
        NEXT j
    NEXT i
    PRINT line
    FOR i = 0 TO 9
        line = "|"
        FOR j = 0 TO 9
            idx = i * 10 + j + 160
            IF idx > 255 THEN
                line = line + "      |"
            ELSE
                ch = CHR$(idx)
                spaces = 6 - LEN(ch)
                pad = ""
                FOR k = 1 TO spaces
                    pad = pad + " "
                NEXT k
                line = line + " " + ch + pad + "|"
            END IF
        NEXT j
        PRINT line
        line = "|"
        FOR j = 0 TO 9
            idx = i * 10 + j + 160
            IF idx > 255 THEN
                line = line + "      |"
            ELSE
                decVal = STR$(idx)
                spaces = 6 - LEN(decVal)
                pad = ""
                FOR k = 1 TO spaces
                    pad = pad + " "
                NEXT k
                line = line + " " + decVal + pad + "|"
            END IF
        NEXT j
        PRINT line
        line = "|"
        FOR j = 0 TO 9
            idx = i * 10 + j + 160
            IF idx > 255 THEN
                line = line + "      |"
            ELSE
                hexVal = HEX$(idx)
                IF LEN(hexVal) = 1 THEN
                    hexVal = "0" + hexVal
                END IF
                hexVal = "$" + hexVal
                spaces = 6 - LEN(hexVal)
                pad = ""
                FOR k = 1 TO spaces
                    pad = pad + " "
                NEXT k
                line = line + " " + hexVal + pad + "|"
            END IF
        NEXT j
        PRINT line
        line = "|"
        FOR j = 0 TO 9
            idx = i * 10 + j + 160
            IF idx > 255 THEN
                line = line + "------+"
            ELSE
                line = line + "------+"
            END IF
        NEXT j
        PRINT line
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "          CONTROL CHARACTERS (0-31)"
    PRINT "============================================"
    PRINT ""
    FOR i = 0 TO 31
        IF i MOD 8 = 0 THEN
            PRINT ""
        END IF
        IF i = 0 THEN section = "NUL"
        IF i = 1 THEN section = "SOH"
        IF i = 2 THEN section = "STX"
        IF i = 3 THEN section = "ETX"
        IF i = 4 THEN section = "EOT"
        IF i = 5 THEN section = "ENQ"
        IF i = 6 THEN section = "ACK"
        IF i = 7 THEN section = "BEL"
        IF i = 8 THEN section = "BS"
        IF i = 9 THEN section = "TAB"
        IF i = 10 THEN section = "LF"
        IF i = 11 THEN section = "VT"
        IF i = 12 THEN section = "FF"
        IF i = 13 THEN section = "CR"
        IF i = 14 THEN section = "SO"
        IF i = 15 THEN section = "SI"
        IF i = 16 THEN section = "DLE"
        IF i = 17 THEN section = "DC1"
        IF i = 18 THEN section = "DC2"
        IF i = 19 THEN section = "DC3"
        IF i = 20 THEN section = "DC4"
        IF i = 21 THEN section = "NAK"
        IF i = 22 THEN section = "SYN"
        IF i = 23 THEN section = "ETB"
        IF i = 24 THEN section = "CAN"
        IF i = 25 THEN section = "EM"
        IF i = 26 THEN section = "SUB"
        IF i = 27 THEN section = "ESC"
        IF i = 28 THEN section = "FS"
        IF i = 29 THEN section = "GS"
        IF i = 30 THEN section = "RS"
        IF i = 31 THEN section = "US"
        PRINT section + "(" + STR$(i) + ") "
    NEXT i
    PRINT ""
    PRINT "Done"
    FUNCTION = 0
END FUNCTION
