Why using ADODB.Stream with ASCII charset special chars as ä are converted to a?
-
21-12-2020 - |
Question
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!
Solution
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.
OTHER TIPS
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