Chilkat Examples
Languages

Chilkat Software

 

Charset 101

 

Strings in Visual Basic 6.0 - Reading Text Files

Visual Basic 6.0 expects ANSI character data when reading text files. This example demonstrates what you'll get if the text file uses a different encoding, such as utf-8.

Here's the test source code. One file contains the letter É in ANSI (a single byte), the other contains the same letter É, but in utf-8 (two bytes).

Function ReadTextFileContents(filename As String) As String

    Dim fnum As Integer
    fnum = FreeFile()
    Open filename For Input As #fnum
    ReadTextFileContents = Input(LOF(fnum), fnum)
    Close #fnum
    
End Function

Private Sub Command3_Click()

    Text1.Text = ReadTextFileContents("É_ansi.txt")
    Text2.Text = ReadTextFileContents("É_utf8.txt")
    
End Sub

The result looks like this:

What happened with the utf-8?

The answer is that the É in utf-8 is two bytes. VB6 interpreted each byte as a separate ANSI character. É is 0xc3 0x89 in utf-8. If these two bytes are interpreted as ANSI, then 0xC3 = Ã, and 0x89 = ‰ (but the default font was not able to render this character properly).

Next: VB6 Displays ????'s - Why?

© 2000-2022 Chilkat Software, Inc. All Rights Reserved. ..