LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

simple text compression

Root / Submissions / [.]

MZ952Created:
OPTION STRICT
OPTION DEFINT

DEF CMP$(S$)
 DIM I,R$,S0,L=LEN(S$)
 FOR I=0 TO L-1 STEP 2
  S0=ASC(S$[I]) MOD 256<<8
  IF I+1<L THEN
   INC S0,ASC(S$[I+1]) MOD 256
  ENDIF
  PUSH R$,CHR$(S0)
 NEXT I
 RETURN R$
END

DEF EXP$(S$)
 DIM R$,I,L=ASC(S$[0])*256+ASC(S$[1])
 DIM N=L/2-!(L MOD 2)
 FOR I=0 TO N
  INC R$,CHR$(ASC(S$[2+I]) AND 65280>>8)
  INC R$,CHR$(ASC(S$[2+I]) AND 255)
 NEXT I
 IF L MOD 2 THEN IF POP(R$) THEN ENDIF ENDIF
 RETURN R$
END
it's no Huffman coding, but uh, cuts text to around half its size and is fairly quick. the largest S$ may be before the functions break down is uh 65280 characters long. Oh, it only works with character codes between 0-255. 256 and beyond are wrapped to within the range of 0-255.

Wait, so is this 2 to 1 CHR compression? 12Me21 made a much smaller function for this: 2 to 1:
T$ = CHR$(ASC("A")+(ASC("B")<<8))
1 to 2:
X = ASC("?")
A$ = CHR$(X AND 255)
B$ = CHR$(X>>8)
I suppose you could adapt your functions to feed every two characters into these and make your code a lot smaller.

Replying to:HTV04
Wait, so is this 2 to 1 CHR compression? 12Me21 made a much smaller function for this: 2 to 1:
T$ = CHR$(ASC("A")+(ASC("B")<<8))
1 to 2:
X = ASC("?")
A$ = CHR$(X AND 255)
B$ = CHR$(X>>8)
I suppose you could adapt your functions to feed every two characters into these and make your code a lot smaller.
My code works with exactly the same principles. All it does extra is wrap the character code values to the range of 0-255 and store a length char to work on strings with odd-numbered length. (Actually, now that I think about it, all I really need to store is the string_len mod 2. That would work for a string of any size.)