|
python 转码就一行代码(虽说函数已经封装了),vba 实现 encode decode,结果基本跟python的一样
- '''编码转换("我是学员","UTF-8","gbk")
- '''decode(encode("我是学员"),"gbk")
- '''decode(encode("我是学员","gbk"),"gbk")
- Function 编码转换(str, CharSet1, CharSet2)
- 编码转换 = ""
- On Error GoTo err1
- Dim ADODB_Stream
- Set ADODB_Stream = CreateObject("ADODB.Stream")
- Dim Bytes() As Byte
- Dim gbk_txt As String
- With ADODB_Stream
- .Charset = CharSet1
- .Open
- .WriteText str, 0
- .Position = 0
- .Type = 1
- If CharSet1 = CharSet2 Then
- .Position = 0
- Else
- .Position = 3 ''无 bom
- End If
- Bytes = .Read()
- .Close
- .Open
- .Type = 1
- .Write Bytes
- .Position = 0
- .Type = 2
- .Charset = CharSet2
- 编码转换 = .ReadText()
- .Close
- End With
- err1:
- Set ADODB_Stream = Nothing
- End Function
- Function encode(ss, Optional ByVal CharSet1 As String = "UTF-8") As Byte()
- On Error GoTo err1
- Dim ADODB_Stream
- Set ADODB_Stream = CreateObject("ADODB.Stream")
- With ADODB_Stream
- .Charset = CharSet1
- .Open
- .WriteText ss, 0
- .Position = 0
- .Type = 1
- If CharSet1 <> "UTF-8" Then
- .Position = 0
- Else
- .Position = 3 ''无 bom
- End If
- encode = .Read()
- .Close
- End With
- err1:
- Set ADODB_Stream = Nothing
- End Function
- Function decode(Bytes, Optional ByVal CharSet2 As String = "UTF-8") As String
- decode = ""
- On Error GoTo err1
- Dim ADODB_Stream
- Set ADODB_Stream = CreateObject("ADODB.Stream")
- With ADODB_Stream
- .Open
- .Type = 1
- .Write Bytes
- .Position = 0
- .Type = 2
- .Charset = CharSet2
- decode = .ReadText()
- .Close
- End With
- err1:
- Set ADODB_Stream = Nothing
- End Function
复制代码
|
-
评分
-
2
查看全部评分
-
|