LoginLogin
Might make SBS readonly: thread

UTF-8 converter

Root / Submissions / [.]

12Me21Created:
'Convert UCS-2 string to UTF-8 byte array
DEF UTF8(TEXT$)
 DIM BYTES%[0]
 VAR I%
 FOR I%=0 TO LEN(TEXT$)-1
  VAR CHAR%=ASC(TEXT$[I%])
  
  '  7 bit  1 Byte
  IF CHAR%<128 THEN
   '
   PUSH BYTES%,CHAR%
   
  '  11 bit  2 Byte
  ELSEIF CHAR%<2048 THEN
   '
   PUSH BYTES%,192 OR ((CHAR%>>6) AND 31)
   '
   PUSH BYTES%,128 OR (CHAR% AND 63)
   
  '  16 bit  3 Byte
  ELSE
   '
   PUSH BYTES%,224 OR ((CHAR%>>12) AND 15)
   '
   PUSH BYTES%,128 OR ((CHAR%>>6) AND 63)
   '
   PUSH BYTES%,128 OR (CHAR% AND 63)
  ENDIF
  
 NEXT I%
 RETURN BYTES%
END'UTF8

'Convert UTF-8 byte array to UCS-2 string
DEF UTF8$(BYTES%)
 VAR LEN%=LEN(BYTES%)
 VAR TEXT$=""
 VAR I%
 FOR I%=0 TO LEN(BYTES%)-1
  VAR CHAR%=BYTES%[I%]
  
  ' 1 Byte  7 bit
  IF CHAR%<128 THEN
   PUSH TEXT$,CHR$(CHAR%)
  
  '  2 Byte  11 bit
  ELSEIF CHAR%<224 THEN
   PUSH TEXT$,CHR$(((CHAR% AND 31)<<6)OR(BYTES%[I%+1] AND 63))
   INC I%
   
  '   3 Byte  16 bit
  ELSE
   PUSH TEXT$,CHR$(((CHAR%AND 15)<<12)OR((BYTES%[I%+1]AND 63)<<6)OR(BYTES%[I%+2]AND 63))
   INC I%,2
  ENDIF
  
 NEXT I%
 RETURN TEXT$
END'UTF8$

No posts yet (will you be the first?)