Convert String to UTF8

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
rullbandspelare
Pro Scripter
Posts: 149
Joined: Tue Mar 23, 2004 9:11 pm

Convert String to UTF8

Post by rullbandspelare » Wed Apr 07, 2021 11:08 am

Hi!
I am struggling to convert a string to UTF-8
I found some code that should do this . But it does not work.
Any input is appreciated!

Code: Select all

VBSTART
Private Function StringToUTF8(String)
    Dim objStream: Set objStream = CreateObject("ADODB.Stream")
    Call objStream.Open()
    objStream.Type = 2
    objStream.Charset = "iso-8859-1"
    Call objStream.WriteText(String)
    objStream.Position = 0
    objStream.Charset = "UTF-8"
    StringToUTF8 = objStream.ReadText()
    Call objStream.Close(): Set objStream = Nothing
End Function
VBEND

//res > vad
VBEval>StringToUTF8("vad"),res

//res > v
VBEval>StringToUTF8("våd"),res

User avatar
Grovkillen
Automation Wizard
Posts: 1128
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Convert String to UTF8

Post by Grovkillen » Wed Apr 07, 2021 12:10 pm

Don't know if this is of any help but this is something that I use:

Code: Select all

GoTo>REPLACE_SWEDISH_CHARACTERS,{"name_of_string"}

SRT>REPLACE_SWEDISH_CHARACTERS
  Let>RSW_TEMP=%REPLACE_SWEDISH_CHARACTERS_Var_1%
  //Ã¥=å
  StringReplace>RSW_TEMP,Ã¥,å,RSW_TEMP
  //Ã…=Å
  StringReplace>RSW_TEMP,Ã…,Å,RSW_TEMP
  //ä=ä
  StringReplace>RSW_TEMP,ä,ä,RSW_TEMP
  //Ä=Ä
  StringReplace>RSW_TEMP,Ä,Ä,RSW_TEMP
  //ö=ö
  StringReplace>RSW_TEMP,ö,ö,RSW_TEMP
  //Ö=Ö
  StringReplace>RSW_TEMP,Ö,Ö,RSW_TEMP
  //é=é
  StringReplace>RSW_TEMP,é,é,RSW_TEMP
  //É=É
  StringReplace>RSW_TEMP,É,É,RSW_TEMP
  //ø=ø
  StringReplace>RSW_TEMP,ø,ø,RSW_TEMP
  //Ø=Ø
  StringReplace>RSW_TEMP,ظ,Ø,RSW_TEMP
  //æ=æ
  StringReplace>RSW_TEMP,æ,æ,RSW_TEMP
  //Æ=Æ
  StringReplace>RSW_TEMP,Æ,Æ,RSW_TEMP
  Let>%REPLACE_SWEDISH_CHARACTERS_Var_1%=RSW_TEMP
END>REPLACE_SWEDISH_CHARACTERS
Last edited by Grovkillen on Wed Apr 07, 2021 1:33 pm, edited 1 time in total.
Let>ME=%Script%

Running: 15.0.27
version history

rullbandspelare
Pro Scripter
Posts: 149
Joined: Tue Mar 23, 2004 9:11 pm

Re: Convert String to UTF8

Post by rullbandspelare » Wed Apr 07, 2021 12:35 pm

Tack for the tip!

That is how i have done it. But You will get a surprise for á, ø and ç
It will be a long list to cover all characters.

rullbandspelare
Pro Scripter
Posts: 149
Joined: Tue Mar 23, 2004 9:11 pm

Re: Convert String to UTF8

Post by rullbandspelare » Fri Apr 09, 2021 9:01 am

SOLVED!
This converts a string to UTF8 i.e "Å Ä Ö" -> "Ã… Ä Ö"

Code: Select all

VBSTART
Private Function StringToUTF8(String)
    Dim objStream: Set objStream = CreateObject("ADODB.Stream")
    Call objStream.Open()
    objStream.Type = 2
    objStream.Charset = "utf-8"
    Call objStream.WriteText(String)
    objStream.Position = 0
    objStream.Charset = "iso-8859-1"
    StringToUTF8 = objStream.ReadText()
    Call objStream.Close(): Set objStream = Nothing
End Function
VBEND

Let>string=Rüllbäñdspèláreë
VBEval>StringToUTF8("%string%"),string
StringReplace>string,,,UTF8string

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts