02
2019
08

字符串二进制流互转

Public  Function Bytes2Str(vin,charset)
        Dim ms,strRet
        Set ms = Server.CreateObject("ADODB.Stream")    '建立流对象
        ms.Type = 1             ' Binary
        ms.Open                    
        ms.Write vin            '把vin写入流对象中
        
        ms.Position = 0         '设置流对象的起始位置是0 以设置Charset属性
        ms.Type = 2              'Text
        ms.Charset = charset    '设置流对象的编码方式为 charset
        strRet = ms.ReadText    '取字符流
        ms.close                '关闭流对象
        Set ms = nothing
        Bytes2Str = strRet
End Function
Public Function Str2Bytes(str,charset)
        Dim ms,strRet
        If lcase(charset)="utf-8" Then
           sPos = 3
        ElseIf lcase(charset)="unicode" Then
           sPos = 2
        Else
           sPos = 0
        End If
        Set ms = CreateObject("ADODB.Stream")    '建立流对象
        ms.Type = 2             ' Text
        ms.Charset = charset    '设置流对象的编码方式为 charset
        ms.Open                    
        ms.WriteText str            '把str写入流对象中
        
        ms.Position = 0         '设置流对象的起始位置是0 以设置Charset属性
        ms.Type = 1              'Binary
        
        ms.Position = sPos                '跳过0-1字节,不同的charset需跳过的位置不同
        vout = ms.Read(ms.Size)    '取字符流
        ms.close                '关闭流对象
        Set ms = nothing
        Str2Bytes = vout
End Function

 

« 上一篇下一篇 »