1

我正在尝试将 UCS2-Little Endian 中现有文件的编码从 Visual Basic 更改为 Windows-1251。

我正在使用以下代码:

Sub convertFile()
t = AlterCharset("utf-16LE", "windows-1251")

iFileNum = FreeFile
Open sFullPath For Output As #iFileNum
Print #iFileNum, t

Close #iFileNum

End Sub

Private Function AlterCharset(FromChaset, ToCharset)
 Dim Bytes
 Bytes = StringToBytes(FromChaset)
 AlterCharset = BytesToString(Bytes, ToCharset)
End Function

Function StringToBytes(Charset)
 Dim Stream: Set Stream = CreateObject("ADODB.Stream")
 Stream.Type = adTypeText
 Stream.Charset = Charset
 Stream.Open
 Stream.LoadFromFile sFullPath
 Stream.flush
 Stream.Position = 0
 Stream.Type = adTypeBinary
 StringToBytes = Stream.Read
 Stream.Close
 Set Stream = Nothing
End Function

Private 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
 Stream.Type = adTypeText
 BytesToString = Stream.ReadText
 Stream.Close
 Set Stream = Nothing

结束功能

问题是我总是在文本的开头得到一些字节,我无法在记事本++中打开它

有人可以看看我的代码有什么问题吗?

4

1 回答 1

0

您可以这样做来阅读 UCS2-Little Endian:

Dim fso     : Set fso = CreateObject("Scripting.FileSystemObject")
txt = fso.OpenTextFile("txtfile.txt", 1, False, -1).ReadAll

进入内存后,您可以写入不同的文件

于 2014-10-24T18:11:57.330 回答