Run-length encoding

You are encouraged to solve this task according to the task description, using any language you may know.


Task

Given a string containing uppercase characters (A-Z), compress repeated 'runs' of the same character by storing the length of that run, and provide a function to reverse the compression.

The output can be anything, as long as you can recreate the input with it.



Example

 Input: WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
 Output: 12W1B12W3B24W1B14W


Note: the encoding step in the above example is the same as a step of the Look-and-say sequence.




FUNCTION RLDecode (i AS STRING) AS STRING
    DIM Loop0 AS LONG, rCount AS STRING, outP AS STRING, m AS STRING
 
    FOR Loop0 = 1 TO LEN(i)
        m = MID$(i, Loop0, 1)
        SELECT CASE m
            CASE "0" TO "9"
                rCount = rCount & m
            CASE ELSE
                IF LEN(rCount) THEN
                    outP = outP & STRING$(VAL(rCount), m)
                    rCount=""
                ELSE
                    outP = outP & m
                END IF
        END SELECT
    NEXT
    FUNCTION = outP
END FUNCTION
 
FUNCTION RLEncode (i AS STRING) AS STRING
    DIM tmp1 AS STRING, tmp2 AS STRING, outP AS STRING
    DIM Loop0 AS LONG, rCount AS LONG
 
    tmp1 = MID$(i, 1, 1)
    tmp2 = tmp1
    rCount = 1
 
    FOR Loop0 = 2 TO LEN(i)
        tmp1 = MID$(i, Loop0, 1)
        IF tmp1 <> tmp2 THEN
            outP = outP & TRIM$(STR$(rCount)) & tmp2
            tmp2 = tmp1
            rCount = 1
        ELSE
            INCR rCount
        END IF
    NEXT
 
    outP = outP & TRIM$(STR$(rCount))
    outP = outP & tmp2
    FUNCTION = outP
END FUNCTION
 
FUNCTION PBMAIN () AS LONG
    DIM initial AS STRING, encoded AS STRING, decoded AS STRING
    initial = INPUTBOX$("Type something.")
    encoded = RLEncode(initial)
    decoded = RLDecode(encoded)
    'in PB/Win, "?" = MSGBOX; in PB/DOS & PB/CC. "?" = PRINT
    ? initial & $CRLF & encoded & $CRLF & decoded
END FUNCTION