I have a string assigned to variable that's encoded as ansi, for example str = "Пирг"
How can I encode it to UTF-8?
Asked
Active
Viewed 2.4k times
3

user2136786
- 305
- 2
- 3
- 11
-
See also [this newer question](/q/28834528/1178314). – Frédéric Jan 21 '19 at 16:00
1 Answers
2
You mean when writing it to a file? Like this:
Set stream = CreateObject("ADODB.Stream")
stream.Open
stream.Type = 2 'text
stream.Position = 0
stream.Charset = "utf-8"
stream.WriteText str
stream.SaveToFile filename, 2
stream.Close
Edit: If you want the UTF-8 string to go into another variable you could do it like this:
Set stream = CreateObject("ADODB.Stream")
stream.Open
stream.Type = 2 'text
stream.Position = 0
stream.Charset = "utf-8"
stream.WriteText str
stream.Flush
stream.Position = 0
stream.Type = 1 'binary
stream.Read(3) 'skip BOM
utfStr = stream.Read
stream.Close

Ansgar Wiechers
- 193,178
- 25
- 254
- 328
-
1No I don't want to write to file. I'm passing this variable to URL to server which accept only utf-8 encoded strings. For example if I urlencode the string, or use `Escape()` function, server returns false status. – user2136786 Mar 05 '13 at 17:35