将当前字符集转换为windows-1252

时间:2017-10-02 21:07:26

标签: vbscript character-encoding

您好我在vbs中有一个脚本,可以将电子邮件发送到所需的目的地

问题是输出文本已损坏

当前输出: ils n ont

期望的输出: ils n'ont等...

经过研究,我发现我需要将文本转换为Windows 1252才能接受法语字符

所以我实现了转换函数:

Const adTypeBinary = 1
Const adTypeText = 2

//accept a string and convert it to Bytes array in the selected Charset
Function StringToBytes(Str,Charset)
  Dim Stream : Set Stream = CreateObject("ADODB.Stream")
  Stream.Type = adTypeText
  Stream.Charset = Charset
  Stream.Open
  Stream.WriteText Str
  Stream.Flush
  Stream.Position = 0
  // rewind stream and read Bytes
  Stream.Type = adTypeBinary
  StringToBytes= Stream.Read
  Stream.Close
  Set Stream = Nothing
End Function

//accept Bytes array and convert it to a string using the selected charset
Function BytesToString(Bytes, Charset)
  Dim Stream : Set Stream = CreateObject("ADODB.Stream")
  Stream.Charset = Charset
  Stream.Type = adTypeBinary
  Stream.Open
  Stream.Write Bytes
  Stream.Flush
  Stream.Position = 0
   // rewind stream and read text
  Stream.Type = adTypeText
  BytesToString= Stream.ReadText
  Stream.Close
  Set Stream = Nothing
End Function

' This will alter charset of a string from 1-byte charset(as windows-1252)
' to another 1-byte charset(as windows-1251)
Function AlterCharset(Str, FromCharset, ToCharset)
  Dim Bytes
  Bytes = StringToBytes(Str, FromCharset)
  AlterCharset = BytesToString(Bytes, ToCharset)
End Function

但是如何检测当前的编码charest以便将它们转换为windows-1252?

我应该如何正确调用我的功能?

  objEmail.Subject  = eSubject 
objEmail.Textbody = AlterCharset(eTextBody , "How to detect the current charset? ", "windows-1252") <--- how to write it properly here?    

编辑:

执行这行代码确实解决了我的char问题:

objEmail.Textbody = AlterCharset(eTextBody , "windows-1252 ", "UTF-8")

但脚本可以在不同的PC上运行,所以我需要动态获取当前的字符集才能将其转换为UTF-8

0 个答案:

没有答案