为什么使用带有 ASCII 字符集的 ADODB.Stream 将 ä 等特殊字符转换为 a?

Dan*_*uez 0 vb6 ascii ado character-encoding codepages

我在尝试将 vb6 中的某些变量的内容输出到文本文件中时遇到问题。问题是,当扩展 ASCII 中的特殊字符显示为 \xc3\xa4、\xc3\xbc、\xc3\xa1 时,它会在输出中转换为匹配的基本 ASCII 字符,如 a、u、a。

\n\n

我尝试像 UTF-8 一样导出它,然后正确显示字符,但我需要输出为 ASCII。另外,对我来说看起来很奇怪的是文件名通常可以包含此字符(\xc3\xa4、\xc3\xbc、\xc3\xa1...)而无需替换。

\n\n

这是因为“ASCII”字符集只是基本字符集而不是扩展字符集吗?也许是因为 Windows 中配置的代码页?我尝试过其中的几个(德语、英语),结果相同。

\n\n

这是我正在使用的代码:

\n\n
Set fileStream = New ADODB.Stream\nIf Not fileStream Is Nothing Then\n    inputString = textPreAppend + inputString\n    fileStream.charSet = "ASCII"\n    fileStream.Open\n    fileStream.WriteText inputString\n    fileStream.Flush\n    fileStream.SaveToFile fileName, adSaveCreateOverWrite\n    fileStream.Flush\n    fileStream.Close\nEnd If\nSet fileStream = Nothing\n
Run Code Online (Sandbox Code Playgroud)\n\n

提前致谢!

\n

Bob*_*b77 5

PRB :ADO 流对象的字符集属性可能需要 Microsoft Internet Explorer 升级字符集属性 (ADO) 都表明 ADO CharSet 值如下所列HKEY_CLASSES_ROOT\\MIME\\Database\\Charset,但这显然不是全部内容。

\n\n

例如,值“ascii”和“us-ascii”都作为“iso-8859-1”的别名列出,但是在我的语言环境设置为美国英语的情况下运行,它们的行为就像 7 位 ASCII MIME 类型。他们几乎不得不这样做,因为无论如何都没有提供任何其他内容来请求 7 位 ASCII 编码。

\n\n

这会产生您似乎想要的结果:

\n\n
Option Explicit\n\nPrivate Sub Main()\n    Dim fileStream As ADODB.Stream\n    Dim inputString As String\n    Const FileName As String = "outputfile.txt"\n\n    Set fileStream = New ADODB.Stream\n    inputString = "The thing is that when a special character" & vbNewLine _\n                & "from extended ASCII appears as \xc3\xa4, \xc3\xbc, \xc3\xa1 it" & vbNewLine _\n                & "is transformed in the output to the matching" & vbNewLine _\n                & "basic ASCII char like a, u, a." & vbNewLine\n    With fileStream\n        .Type = adTypeText\n        .Charset = "iso-8859-1"\n        .Open\n        .WriteText inputString\n        .SaveToFile FileName, adSaveCreateOverWrite\n        .Close\n    End With\nEnd Sub\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出:

\n\n
The thing is that when a special character\nfrom extended ASCII appears as \xc3\xa4, \xc3\xbc, \xc3\xa1 it\nis transformed in the output to the matching\nbasic ASCII char like a, u, a.\n
Run Code Online (Sandbox Code Playgroud)\n\n

我们必须假设要求“ascii”(这些值都是小写的,但显然不区分大小写)意味着 7 位 ASCII,可能是本地化的。

\n\n

除非您需要UTF-8,否则使用 UTF-8 不是一个好主意。虽然许多 *nix 系统假装没有区别,但 Stream 会写入 BOM,当然那些扩展(非 ASCII)字符是多字节编码的。

\n