VBCorLib icon indicating copy to clipboard operation
VBCorLib copied to clipboard

File.ReadAllText is not correct.

Open halfsprit opened this issue 3 years ago • 1 comments

if file is gbk encoding, and the last char is double bytes, File.ReadAllText can't get correct string. s = File.ReadAllText("e:\aa.js", Encoding.GetEncoding("GBK"))

halfsprit avatar May 28 '21 16:05 halfsprit

I found the issue, unfortunately it is not a simple fix. The problem is that the Encoder that is being used relies on the Win32 IsDBCSLeadByteEx method. Turns out that method can only tell you if the byte you are checking is probably a lead byte, which can lead false positives. So dependency on that method has to be removed.

If you still want to read an entire file using the encoder, it can be done using:

    Dim En      As Encoding
    Dim Bytes() As Byte
    Dim s       As String
        
    Bytes = File.ReadAllBytes("e:\aa.js")
    Set En = Encoding.GetEncoding("GBK")    
    s = En.GetString(Bytes)

kellyethridge avatar Nov 28 '21 21:11 kellyethridge