如何在 VBScript 中创建 UTF-16 文件? [英] How to create UTF-16 file in VBScript?
问题描述
我的系统是 Window 10 English-US.我需要将一些不可打印的 ASCII 字符写入文本文件.因此,例如对于 28 的 ASCII 值,我想将 \u001Cw 写入文件.在用 Java 编码时,我不需要做任何特别的事情.下面是我在 VBS 中的代码
My system is Window 10 English-US. I need to write some non-printable ASCII characters to a text file. So for eg for the ASCII value of 28, I want to write \u001Cw to the file. I don't have to do anything special when coded in Java. Below is my code in VBS
Dim objStream
Set objStream = CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 2
objStream.Position = 0
objStream.CharSet = "utf-16"
objStream.WriteText ChrW(28) 'Need this to appear as \u001Cw in the output file
objStream.SaveToFile "C:\temp\test.txt", 2
objStream.Close
推荐答案
您需要一个读写流,以便写入它并将其保存到文件中.
You need a read-write stream so that writing to it and saving it to file both work.
Const adModeReadWrite = 3
Const adTypeText = 2
Const adSaveCreateOverWrite = 2
Sub SaveToFile(text, filename)
With CreateObject("ADODB.Stream")
.Mode = adModeReadWrite
.Type = adTypeText
.Charset = "UTF-16"
.Open
.WriteText text
.SaveToFile filename, adSaveCreateOverWrite
.Close
End With
End Sub
text = Chr(28) & "Hello" & Chr(28)
SaveToFile text, "C:\temp\test.txt"
其他注意事项:
- 我喜欢用
Const
明确定义代码中的所有常量.让阅读变得如此轻松. - 一个
With
块在这里节省了很多输入. - 将流类型设置为
adTypeText
并不是真正必要的,无论如何这是默认设置.但我想,显式优于隐式. - 在新流上将
Position
设置为 0 是多余的. - 没有必要对 ASCII 范围字符使用
ChrW()
.当您将流保存到文件时,流的Charset
决定了字节宽度.在 RAM 中,无论如何都是 Unicode(是的,即使在 VBScript 中). - ADODB.Stream 支持两种 UTF-16 编码:little-endian
UTF-16LE
(这是默认值,与UTF-16
同义)和 big-endianUTF-16BE
,字节顺序颠倒.
- I like to explicitly define with
Const
all the constants in the code. Makes reading so much easier. - A
With
block save quite some typing here. - Setting the stream type to
adTypeText
is not really necessary, that's the default anyway. But explicit is better than implicit, I guess. - Setting the
Position
to 0 on a new stream is superfluous. - It's unnecessary to use
ChrW()
for ASCII-range characters. The stream'sCharset
decides the byte width when you save the stream to file. In RAM, everything is Unicode anyway (yes, even in VBScript). - There are two UTF-16 encodings supported by ADODB.Stream: little-endian
UTF-16LE
(which is the default and synonymous withUTF-16
) and big-endianUTF-16BE
, with the byte order reversed.
您可以使用 FileSystemObject 及其 CreateTextFile()
方法:
You can achieve the same result with the FileSystemObject and its CreateTextFile()
method:
Set FSO = CreateObject("Scripting.FileSystemObject")
Sub SaveToFile(text, filename)
' CreateTextFile(filename [, Overwrite [, Unicode]])
With FSO.CreateTextFile(filename, True, True)
.Write text
.Close
End With
End Sub
text = Chr(28) & "Hello" & Chr(28)
SaveToFile text, "C:\temp\test.txt"
这有点简单,但它只提供了一个布尔 Unicode
参数,它在 UTF-16 和 ANSI 之间切换(不是 ASCII,因为文档错误地声称!).ADODB.Stream
的解决方案为您提供了细粒度的编码选择,例如 UTF-8,这在 FileSystemObject 中是不可能的.
This is a little bit simpler, but it only offers a Boolean Unicode
parameter, which switches between UTF-16 and ANSI (not ASCII, as the documentation incorrectly claims!). The solution with ADODB.Stream
gives you fine-grained encoding choices, for example UTF-8, which is impossible with the FileSystemObject.
为了记录,有两种方法可以创建 UTF-8 编码的文本文件:
For the record, there are two ways to create an UTF-8-encoded text file:
- Microsoft 喜欢采用的方式,在文件开头使用 3 字节长的字节顺序标记 (BOM).大多数(如果不是所有)Microsoft 工具在提供UTF-8"作为选项时都会这样做,ADODB.Stream 也不例外.
- 其他人的做法 - 没有 BOM.这对于大多数用途都是正确的.
要创建带有 BOM 的 UTF-8 文件,可以使用上面的第一个代码示例.要创建 没有 BOM 的 UTF-8 文件,我们可以使用两个流对象:
To create an UTF-8 file with BOM, the first code sample above can be used. To create an UTF-8 file without BOM, we can use two stream objects:
Const adModeReadWrite = 3
Const adTypeBinary = 1
Const adTypeText = 2
Const adSaveCreateOverWrite = 2
Sub SaveToFile(text, filename)
Dim iStr: Set iStr = CreateObject("ADODB.Stream")
Dim oStr: Set oStr = CreateObject("ADODB.Stream")
' one stream for converting the text to UTF-8 bytes
iStr.Mode = adModeReadWrite
iStr.Type = adTypeText
iStr.Charset = "UTF-8"
iStr.Open
iStr.WriteText text
' one steam to write bytes to a file
oStr.Mode = adModeReadWrite
oStr.Type = adTypeBinary
oStr.Open
' switch first stream to binary mode and skip UTF-8 BOM
iStr.Position = 0
iStr.Type = adTypeBinary
iStr.Position = 3
' write remaining bytes to file and clean up
oStr.Write iStr.Read
oStr.SaveToFile filename, adSaveCreateOverWrite
oStr.Close
iStr.Close
End Sub
这篇关于如何在 VBScript 中创建 UTF-16 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!