Escaping non-ASCII characters (or how to remove the BOM?)

后端 未结 2 1362
一个人的身影
一个人的身影 2021-01-13 04:16

I need to create an ANSI text file from an Access recordset that outputs to JSON and YAML. I can write the file, but the output is coming out with the original characters, a

2条回答
  •  野的像风
    2021-01-13 05:12

    ... ok .... i found some example code on how to remove the BOM. I would have thought it would be possible to do this more elegantly when actually writing the text in the first place. Never mind. The following code removes the BOM.

    (This was originally posted by Simon Pedersen at http://www.imagemagick.org/discourse-server/viewtopic.php?f=8&t=12705)

    ' Removes the Byte Order Mark - BOM from a text file with UTF-8 encoding
    ' The BOM defines that the file was stored with an UTF-8 encoding.
    
    Public Function RemoveBOM(filePath)
    
        ' Create a reader and a writer
                Dim writer, reader, fileSize
                Set writer = CreateObject("Adodb.Stream")
                Set reader = CreateObject("Adodb.Stream")
    
        ' Load from the text file we just wrote
                reader.Open
                reader.LoadFromFile filePath
    
        ' Copy all data from reader to writer, except the BOM
                writer.Mode = 3
                writer.Type = 1
                writer.Open
                reader.Position = 5
                reader.CopyTo writer, -1
    
        ' Overwrite file
                writer.SaveToFile filePath, 2
    
        ' Return file name
                RemoveBOM = filePath
    
        ' Kill objects
                Set writer = Nothing
                Set reader = Nothing
        End Function
    

    It might be useful for someone else.

提交回复
热议问题