将字符串转换为UTF-8

时间:2013-03-05 17:14:54

标签: vbscript

我有一个字符串分配给编码为ansi的变量,例如str = "Пирг"
如何将其编码为UTF-8?

1 个答案:

答案 0 :(得分:2)

您的意思是将其写入文件?像这样:

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

编辑:如果您希望UTF-8字符串进入另一个变量,您可以这样做:

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