zoukankan      html  css  js  c++  java
  • VBS字符串在不同编码之间的转换

    vbscript, vbs 字符串在不同编码之间的转换,包括 ISO-8859-1,UTF-8,GBK,GB2312,GB18030之间的转换.

    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

    使用例子:

    dim s1,s2,FromCharset,ToCharset
    s1 = "我的字符串"
    FromCharset = "GB2312"
    ToCharset = "ISO-8859-1"
    s2 = AlterCharset(s1,FromCharset,ToCharset)
    

      

  • 相关阅读:
    Mac OS中使用VScode配置C语言开发环境
    图形数据库(GraphDB)
    将博客搬至CSDN
    Nodejs课堂笔记-第四课 Dynamodb为何物
    Nodejs课堂笔记-第三课 构建一个nodejs的Docker镜像
    Nodejs课堂笔记-第二课 package.json的作用
    MyBatis 3(中文版) 第四章 使用注解配置SQL映射器
    idea hibernate jpa 生成实体类
    Maven常用配置
    spring发送邮件
  • 原文地址:https://www.cnblogs.com/equation/p/15234294.html
Copyright © 2011-2022 走看看