How to determine whether a VB6 code file is encoded in ANSI or UTF-8 ?
In my code editor, I usually use RC6.New_c.FSO.ReadTextContent(sFileName, , CP_UTF8) to open VB6 code files. That said, my code editor opens all VB6 code files in UTF-8.
However, if the VB code file is encoded in ANSI, then when the VB code file is opened using the UTF-8, the Chinese characters in the VB code files will become garbled.
I'd like to know, how to get the encoding format of the VB6 code files before opening them? Thanks!
Re: How to determine whether a VB6 code file is encoded in ANSI or UTF-8 ?
If it's an ASCII file, then there's some confusion because ASCII is a perfect subset of UTF-8. If it's truly ANSI (with some of the characters using the 8th bit), but being interpreted as UTF-8, that can be problematic.
But, are you sure you don't mean UTF-16?
To my understanding, without a BOM marker, there's no bullet-proof way to differentiate ANSI from UTF-8. It's best if you just know which it is beforehand, and treat it as such when you read/write it.
Just to say it, there are certain bit-sequences that wouldn't be allowed in a UTF-8 file. So, at certain times, we can detect that a file is not UTF-8. However, pretty much all bit-sequences are allowed in an ANSI file. So, an ANSI file that doesn't use the non-allowed UTF-8 bit sequences, may be erroneously interpreted as a UTF-8 file. That's the problem.
Last edited by Elroy; May 17th, 2024 at 12:20 PM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Re: How to determine whether a VB6 code file is encoded in ANSI or UTF-8 ?
Originally Posted by SearchingDataOnly
Thank you, Elroy.
I uploaded a test code, please check.
Sorry, but I don't do any RC#'ing. You may want to ask Schmidt.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
I've just tested your CTextFile.cls and it seems to be able to automatically determine the format of the code file. But this class needs to use Vec06BAsConv.tlb. Could you provide a version that doesn't use Vec06BAsConv.tlb? Thanks.
Last edited by SearchingDataOnly; May 18th, 2024 at 02:18 PM.
Re: How to determine whether a VB6 code file is encoded in ANSI or UTF-8 ?
Originally Posted by SearchingDataOnly
You can use any method you are familiar with to read ModANSI.bas into a TextBox
SDO, this is confusing. I'm not having any problems reading ASCII/ANSI/Unicode files. I'm aware I can read ModANSI.bas as a data file. Not sure I have any need to though.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Re: How to determine whether a VB6 code file is encoded in ANSI or UTF-8 ?
Originally Posted by Elroy
SDO, this is confusing. I'm not having any problems reading ASCII/ANSI/Unicode files. I'm aware I can read ModANSI.bas as a data file. Not sure I have any need to though.
Elroy, could you correctly display the Chinese characters in theModANSI.bas in the TextBox?
Re: How to determine whether a VB6 code file is encoded in ANSI or UTF-8 ?
Sure, VB6 is inherently ANSI:
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Re: How to determine whether a VB6 code file is encoded in ANSI or UTF-8 ?
Not sure that those are actually "Chinese" though, as I don't know Chinese. I suspect they're not.
However, VB6's code window is never Unicode of any "brand". So, I'm quite unclear as to what you're trying to do.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Re: How to determine whether a VB6 code file is encoded in ANSI or UTF-8 ?
The trick's CTextFile.cls seems to be able to automatically determine the encoding format of the vb6 code files, but it needs to use Vec06BAsConv.tlb. I don't want to use an external tlb.
Code:
Private Sub CmdTheTrick_Click()
Dim oFile As New cTextFile
oFile.LoadTextFile App.Path & "\ModANSI.bas"
Text1.Text = oFile.Content
End Sub
Last edited by SearchingDataOnly; May 18th, 2024 at 02:20 PM.
Re: How to determine whether a VB6 code file is encoded in ANSI or UTF-8 ?
Those Chinese characters will display correctly only if your Windows is in Chinese! For everyone else they will display as garbage as in Elroy's screenshot!
Re: How to determine whether a VB6 code file is encoded in ANSI or UTF-8 ?
Originally Posted by VanGoghGaming
Those Chinese characters will display correctly only if your Windows is in Chinese! For everyone else they will display as garbage as in Elroy's screenshot!
Yes. However, if these files are not read using the correct encoding format, Chinese characters will appear garbled even on Chinese computers.