Strencode

This function transforms a UCS2 encoded character string into another format that is given by an additional parameter. It returns the number of character found in the destination.

Syntax

CODECODE CODEshSTRING_LENGTH=StrEncode(STRING_SOURCE,VAR_DESTINATION,TYPE)

Any other given value will trigger an error.

Example

CODECODE CODEsh# Let's transcode a character string from one format to another# SOURCE_FORMAT should be "ASCII", "UCS2",or "UTF8"# DESTINATION_FORMAT should be "ASCII", "UCS2",or "UTF8"Funprog TRANSCODE(SOURCE, SOURCE_FORMAT, DESTINATION,DESTINATION_FORMAT)Const Clbfile SOURCEVariable ClbFile DESTINATIONConst Char SOURCE_FORMAT,DESTINATION_FORMATLocal Integer I# Let's first look if the format is the sameIf toupper(SOURCE_FORMAT)=toupper(DESTINATION_FORMAT)DESTINATION=SOURCEEnd len(DESTINATION)Endif# Now let's use one or two transcodingsCase toupper(SOURCE_FORMAT)-toupper(DESTINATION_FORMAT)When "ASCII UCS2" : I=strDecode(SOURCE,DESTINATION,50)When "UTF8 UCS2" : I=strDecode(SOURCE,DESTINATION,0)When "UCS2 ASCII" : I=strEncode(SOURCE,DESTINATION,50)When "UCS2 UTF8" : I=strEncode(SOURCE,DESTINATION,0)When Default : Local Integer ISIZEISIZE=max(0,int(1+log(max(1,len(SOURCE)+1))/log(2))-9)Local Clbfile INTERMEDIATE(ISIZE)Case toupper(SOURCE_FORMAT)-toupper(DESTINATION_FORMAT)When "ASCII UTF8" : I=strDecode(SOURCE,INTERMEDIATE,50)I=strEncode(INTERMEDIATE,DESTINATION,0)When "UTF8 ASCII" : I=strDecode(SOURCE,INTERMEDIATE,0)I=strEncode(INTERMEDIATE,DESTINATION,50)When Default : # Unknown conversionEnd -1EndcaseEndcaseEnd I# Let's create a blob containing an ascii text# The Clbfile text is always stoed in memory in UCS2 formatLocal Clbfile TEXT(0)Local Blbfile BLOB_TEXT(0)Local Integer ITEXT="This is a text"+chr$(10)+"with several lines"+chr$(10)+"including accents"+"I asked for an 'à la carte menu'"I=strEncode(TEXT,BLOB_TEXT,50)

See also

B64Decode, B64Encode, strDecode