Page 1 of 1

Convert Variable to UTF8, how?

Posted: Tue Sep 25, 2012 12:58 pm
by fingus
I need to Convert a Variable to UTF8-Character, how to do that in Hollywood?

setdefaultencoding doesn´t work.

Re: Convert Variable to UTF8, how?

Posted: Tue Sep 25, 2012 8:06 pm
by PEB
Try this:

Code: Select all

SetDefaultEncoding(#ENCODING_UTF8)

EnDash$=Chr($e2)..Chr($80)..Chr($93)
EmDash$=Chr($e2)..Chr($80)..Chr($94)
Ellipsis$=Chr($e2)..Chr($80)..Chr($a6)
OpenQuoteSingle$=Chr($e2)..Chr($80)..Chr($98)
CloseQuoteSingle$=Chr($e2)..Chr($80)..Chr($99)
OpenQuoteDouble$=Chr($e2)..Chr($80)..Chr($9c)
CloseQuoteDouble$=Chr($e2)..Chr($80)..Chr($9d)

SetFont(#SERIF, 24)
SetFontStyle(#NORMAL)
SetFontStyle(#ANTIALIAS)

TextOut(0, 0, "This is an En Dash: "..EnDash$.."\n".."This is an Em Dash: "..EmDash$.."\n".."This is an Ellipsis: "..Ellipsis$.."\n".."This is an open quote: "..OpenQuoteDouble$.."\n".."This is a close quote: "..CloseQuoteDouble$)

Re: Convert Variable to UTF8, how?

Posted: Fri Oct 05, 2012 8:34 pm
by lazi
I am using these functions to convert from and to UTF8.

Code: Select all

utf8= {
        $C381,
        $C3A1,
        $C389,
        $C3A9,
        $C38D,
        $C3AD,
        $C393,
        $C3B3,
        $C396,
        $C3B6,
        $C590,
        $C591,
        $C39A,
        $C3BA,
        $C39C,
        $C3BC,
        $C5B0,
        $C5B1,
        0
        }

latin= {
        $C1,
        $E1,
        $C9,
        $E9,
        $CD,
        $ED,
        $D3,
        $F3,
        $D6,
        $F6,
        $D5,
        $F5,
        $DA,
        $FA,
        $DC,
        $FC,
        $DB,
        $FB,
        0
        }

Function utf82l(data)
Local j, ck,uts,lat

lat=""

for j=0 to strlen(data)-1

      ck=asc(midstr(data,j,1))

      if ck & 128

        ut=(ck << 8)+asc(midstr(data,j+1,1))
        j=j+1

        uts=0
        while (utf8[uts]<>ut) and (uts<18)
          uts=uts+1
        wend
        lat=lat .. Chr(latin[uts])
      else
        lat=lat .. Chr(ck)
      endif

Next
Return(lat)
EndFunction

Function l2utf8(data)
Local j, ck,uts,lat
uts=""

For j=0 To StrLen(data)-1
    ck=Asc(MidStr(data,j,1))
    If ck > 128

        lat=0
        While (latin[lat]<>ck) and (lat<18)
          lat=lat+1
        wend
        uts=uts .. Chr(utf8[lat] >> 8) .. Chr(utf8[lat]&255)

     Else
        uts=uts .. Chr(ck)

     EndIf
Next
Return(uts)
EndFunction