Unicode to UTF-8

27,465

Using the Stream object to save your file with the utf-8 charset might work better for you; here's a simple .vbs function you could test out on your data:

Option Explicit

Sub Save2File (sText, sFile)
    Dim oStream
    Set oStream = CreateObject("ADODB.Stream")
    With oStream
        .Open
        .CharSet = "utf-8"
        .WriteText sText
        .SaveToFile sFile, 2
    End With
    Set oStream = Nothing
End Sub

' Example usage: '
Save2File "The data I want in utf-8", "c:\test.txt"
Share:
27,465
Ruslan
Author by

Ruslan

Updated on November 19, 2020

Comments

  • Ruslan
    Ruslan over 3 years

    i'm using vbscript to extract data from db2 and write to file. Writing to file like:

    Set objTextFile = objFSO.CreateTextFile(sFilePath, True, True)
    

    that creates file in unicode. But that is xml file and it uses UTF-8. So when i open xml file with MS XML Notepad it throws error: 'hexadecimal value 0x00 is an invalid character'

    So i opening this text file with TextPad and saving in UTF-8. After that XML opens without any problems. Can i convert file from Unicode to UTF-8 by vbScript?

  • Ekkehard.Horner
    Ekkehard.Horner over 6 years
    That's just utter nonsense/dangerous mis-information. Prepending a BOM to a single byte ANSI encoded string does not magically convert it to UTF-8.