为什么使用 ADODB.Stream 和 ASCII 字符集特殊字符 ä都转换成a?

发布于 2024-11-29 18:27:47 字数 698 浏览 1 评论 0原文

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

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

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

这是我正在使用的代码:

Set fileStream = New ADODB.Stream
If Not fileStream Is Nothing Then
    inputString = textPreAppend + inputString
    fileStream.charSet = "ASCII"
    fileStream.Open
    fileStream.WriteText inputString
    fileStream.Flush
    fileStream.SaveToFile fileName, adSaveCreateOverWrite
    fileStream.Flush
    fileStream.Close
End If
Set fileStream = Nothing

提前致谢!

I'm having a problem trying to output the content of some variables in vb6 into a text file. The thing is that when a special character from extended ASCII appears as ä, ü, á it is transformed in the output to the matching basic ASCII char like a, u, a.

I've tried to export it like UTF-8 and then the character is shown correctly, but I need the output to be ASCII. Also, looks strange for me that the filename can normally contain this chars (ä, ü, á...) without sustitution.

Can this be because "ASCII" charset is just the basic and not the extended? Maybe because of the CodePages configured in Windows? I've tried with a couple of them (German, English) with the same result.

This is the code I'm using:

Set fileStream = New ADODB.Stream
If Not fileStream Is Nothing Then
    inputString = textPreAppend + inputString
    fileStream.charSet = "ASCII"
    fileStream.Open
    fileStream.WriteText inputString
    fileStream.Flush
    fileStream.SaveToFile fileName, adSaveCreateOverWrite
    fileStream.Flush
    fileStream.Close
End If
Set fileStream = Nothing

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相对绾红妆 2024-12-06 18:27:47

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

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

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

Option Explicit

Private Sub Main()
    Dim fileStream As ADODB.Stream
    Dim inputString As String
    Const FileName As String = "outputfile.txt"

    Set fileStream = New ADODB.Stream
    inputString = "The thing is that when a special character" & vbNewLine _
                & "from extended ASCII appears as ä, ü, á it" & vbNewLine _
                & "is transformed in the output to the matching" & vbNewLine _
                & "basic ASCII char like a, u, a." & vbNewLine
    With fileStream
        .Type = adTypeText
        .Charset = "iso-8859-1"
        .Open
        .WriteText inputString
        .SaveToFile FileName, adSaveCreateOverWrite
        .Close
    End With
End Sub

输出:

The thing is that when a special character
from extended ASCII appears as ä, ü, á it
is transformed in the output to the matching
basic ASCII char like a, u, a.

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

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

Both PRB: Charset Property of ADO Stream Object May Require Microsoft Internet Explorer Upgrade and Charset Property (ADO) suggest that ADO CharSet values are those listed under HKEY_CLASSES_ROOT\MIME\Database\Charset but that clearly is not the entire story.

For example both values "ascii" and "us-ascii" are listed there as aliases of "iso-8859-1" however running with my locale set to U.S. English they act like a 7-bit ASCII MIME type. They'd almost have to, since there is nothing else provided for requesting 7-bit ASCII encoding anyway.

This produces the result you seem to want:

Option Explicit

Private Sub Main()
    Dim fileStream As ADODB.Stream
    Dim inputString As String
    Const FileName As String = "outputfile.txt"

    Set fileStream = New ADODB.Stream
    inputString = "The thing is that when a special character" & vbNewLine _
                & "from extended ASCII appears as ä, ü, á it" & vbNewLine _
                & "is transformed in the output to the matching" & vbNewLine _
                & "basic ASCII char like a, u, a." & vbNewLine
    With fileStream
        .Type = adTypeText
        .Charset = "iso-8859-1"
        .Open
        .WriteText inputString
        .SaveToFile FileName, adSaveCreateOverWrite
        .Close
    End With
End Sub

Output:

The thing is that when a special character
from extended ASCII appears as ä, ü, á it
is transformed in the output to the matching
basic ASCII char like a, u, a.

We have to assume that asking for "ascii" (the values are all lowercased though clearly are not case-sensitive) means 7-bit ASCII, perhaps localized.

Using UTF-8 is a bad idea unless you want UTF-8. While a lot of *nix systems pretend there is no difference, the Stream will write a BOM and of course those extended (non-ASCII) characters are multibyte encoded.

萌无敌 2024-12-06 18:27:47

为什么你不能做如下的事情呢?这对我来说没问题:

    Dim FileNo As Integer
    Dim strFile As String

    strFile = "C:\Test.txt"
    FileNo = FreeFile

    Dim strVariable As String
    strVariable = "some text with extended chars in: ÙÑáêôü"

    Open strFile For Append As #FileNo

    Print #FileNo, strVariable

    Close #FileNo

Why can't you just do something like the following? This works ok for me:

    Dim FileNo As Integer
    Dim strFile As String

    strFile = "C:\Test.txt"
    FileNo = FreeFile

    Dim strVariable As String
    strVariable = "some text with extended chars in: ÙÑáêôü"

    Open strFile For Append As #FileNo

    Print #FileNo, strVariable

    Close #FileNo
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文