Search code examples
encodingutf-8vbscriptdata-conversionutf-16

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


Is there any method to convert UTF-8 to UTF-16 BE in VBscript?

I have this following code, but converts only into UTF-16 LE. I need the output be in 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

Solution

  • 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
    

    If the BOM is needed in the output, we can explicitly add it with

        .Charset = "utf-16be"
        .WriteText ChrW(&hFEFF)
        .WriteText inputStream.ReadText
    

    or, as pointed by Kul-Tigin, we can change the .Charset property from utf-16be to unicodeFEFF.