Here are two program I made out of necessity: I needed to transfer graphics
from one HP-28 to another.  '->RLE' does a byte wise RLE compression of a
string outputting a list of numbers.  The result can be pretty good on
graphics strings (LCD->).  I'd say it'll reduce most graphics strings to a list
of 160 numbers or less.  'RLE->' accepts the list of numbers and gives the
original string back.  Let's start seeing some artwork on here now!

1: string ---->  1: list

'->RLE'

<< -> o                 ! store the string in local variable 'o'.
  << { }                ! intialize empty list.
     0                  ! intialize count to 0.
     o NUM              ! get NUM of the FIRST character in 'o'.
     2 o SIZE FOR j     ! loop from 2 to size of 'o'.
       o j DUP SUB      ! get jth character out of 'o'.
       NUM              ! get its value.
       IF DUP2 <>       ! is it the same as the last character?...
                        ! <> means notequal.
       THEN ROT         ! ...if not, then get count on level 1.
         256 *          ! equivalent of 8 shift lefts.
         ROT +          ! add last character value to shifted count.
         ROT +          ! add it to the list.
         0              ! 0 is new count.
         ROT            ! new "last character" to level 1.
       ELSE DROP        ! ...otherwise, it's the same character, so drop it
         SWAP 1 + SWAP  ! and add one to the count.
       END
     NEXT
     SWAP 256 *         ! end of string, so shift count left 8.
     +                  ! add it to the character value
     SWAP               ! and add that to the list.
  >>
>>

1: list ---> 1: string

'RLE->'

<< LIST->               ! list out.
   ""                   ! initialize empty output string.
   1 ROT START          ! loop from 1 to size of list.
     OVER CHR           ! get character from value from list.
     SWAP 4 ROLL
     256 / IP           ! shift 8 right to get count.
     START              ! loop from 0 to character count.
       OVER +           ! add character to output string.
     NEXT
     SWAP DROP          ! done with character, so drop it.
   NEXT
>>

I've also written a similar compressor that uses LZW compression, but it's
terribly slow and usually does slightly worse on grahics strings than RLE.

-Randy.

