在 Vbscript 中将 UTF-8 文件转换为 UTF-16 BE 文件

Convert UTF-8 file to UTF-16 BE file in Vbscript

有没有什么方法可以在VBscript中将UTF-8转换为UTF-16 BE?

我有以下代码,但只能转换为 UTF-16 LE。 我需要输出为 UTF-16 BE.

Sub Utf8ToUtf16Le(fileIn,fileOut)

Set FSO = CreateObject("Scripting.FileSystemObject")
Set oStream = CreateObject ("ADODB.Stream")

    With oStream
        .Open
        .Type = adTypeText
        .Charset = "utf-8"
        .LoadFromFile fileIn
        FSO.OpenTextFile(fileOut, 2, True, True).Write .ReadText
        .Close
    End With

End Sub
Sub Utf8ToUtf16Le(fileIn,fileOut)
Const adTypeText = 2
Const adSaveCreateOverWrite = 2
Dim inputStream

    Set inputStream = CreateObject("ADODB.Stream")
    With inputStream
        .Open
        .Type = adTypeText
        .Charset = "utf-8"
        .LoadFromFile fileIn
        .Position = 0
    End With 

    With CreateObject("ADODB.Stream")
        .Open
        .Type = adTypeText
        .Charset = "utf-16be"
        .WriteText inputStream.ReadText
        .Position = 0
        .SaveToFile fileOut, adSaveCreateOverWrite
        .Close
    End With
    inputStream.Close

End Sub

如果输出中需要 BOM,我们可以使用

显式添加它
    .Charset = "utf-16be"
    .WriteText ChrW(&hFEFF)
    .WriteText inputStream.ReadText

或者,正如 Kul-Tigin 所指出的,我们可以将 .Charset 属性 从 utf-16be 更改为 unicodeFEFF