REM
REM UTF encoder
REM Encode characters like . ,  to UTF-8.
REM
REM Peter van Eerten - March 2009.
REM Revised November 2009.
REM

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

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

REM Print info about inputstring
PRINT NL$, "ASCII values decimal: ";
FOR x = 1 TO LEN(arg$[1])
    PRINT ASC(MID$(arg$[1], x, 1)), " ";
NEXT

PRINT NL$, "ASCII values hex: ";
FOR x = 1 TO LEN(arg$[1])
    PRINT HEX$(ASC(MID$(arg$[1], x, 1))), " ";
NEXT

PRINT NL$, "ASCII string: ", arg$[1]

LET t = 1

REM Calculate to UTF8
WHILE t <= LEN(arg$[1]) DO

    c = ASC(MID$(arg$[1], t, 1))

    IF c > 127 THEN

        REM Binary AND with 11000000, shift 6 positions to the right, add 11000000 to identify 2nd byte
        b1 = ((c & 192) >> 6) + 192

        REM Binary AND with 00111111, add 10000000 to identify first byte
        b2 = (c & 63) + 128

        REM Add UTF char with ASCII of byte1 + ASCII of byte 2
        new$ = CONCAT$(new$, CHR$(b1))
        new$ = CONCAT$(new$, CHR$(b2))
    ELSE

        new$ = CONCAT$(new$, MID$(arg$[1], t, 1))
    ENDIF

    t = t + 1
WEND

REM Print info about outputstring
PRINT NL$, "UTF-8 values decimal: ";
FOR x = 1 TO LEN(new$)
    PRINT ASC(MID$(new$, x, 1)), " ";
NEXT

PRINT NL$, "UTF-8 values hex: ";
FOR x = 1 TO LEN(new$)
    PRINT HEX$(ASC(MID$(new$, x, 1))), " ";
NEXT

PRINT NL$, "UTF-8 string: ", new$, NL$