VB6,读写Unicode编码的文本文件如何会这样
VB6,读写Unicode编码的文本文件怎么会这样?
在VB6中用一段代码把文本文件1.txt中的半角字符“A”替换为全角字符“A”:
Private Sub Command1_Click()
Dim Str1 As String
Dim Str2 As String
Dim I As Long
Dim StrTmpFile As String
Dim iCompare As Integer
Dim C1 As String
Dim C2 As String
iCompare = vbBinaryCompare
StrTmpFile = App.Path & "\" & "SBC2DBC.tmp"
Open "C:\1.txt" For Input As #1
Open StrTmpFile For Output As #2
While Not EOF(1)
Line Input #1, Str1
Str2 = Str1
C1 = "A"
C2 = StrConv(C1, vbWide)
Str2 = Replace(Str2, C1, C2, , , iCompare)
Print #2, Str2
Wend
Close #1
Close #2
Kill "C:\1.txt"
Name StrTmpFile As "C:\1.txt"
End Sub
现测试文件:C:\1.txt的内容为:3841195078-87113-iMANHpBAcI
当把上述C:\1.txt放入上面字符串,存为Ansi编码时,运行上述程序后没有发现问题,A被成功替换成了“A”
当把上述C:\1.txt放入上面字符串,存为Unicode编码时,运行上述程序后,文件中的内容变成了:
?841195078-87113-iMANHpBAcI
前面的字符3变成了一个不可识别的符号加问号,而且用记事本查看,原来unicode编码的文件变成了Ansi编码。
请问,在VB6中,如何读写一个unicode编码的文本文件?
------解决方案--------------------
Ansi,Unicode(little endian),Unicode big endian,UTF-8编码文件相互转换
http://blog.****.net/chenjl1031/article/details/6059767
------解决方案--------------------
你用的vbWide,那是当然的。你看看我博客里面的是怎么用的。
------解决方案--------------------
把前两个字节去掉
------解决方案--------------------
在VB6中用一段代码把文本文件1.txt中的半角字符“A”替换为全角字符“A”:
Private Sub Command1_Click()
Dim Str1 As String
Dim Str2 As String
Dim I As Long
Dim StrTmpFile As String
Dim iCompare As Integer
Dim C1 As String
Dim C2 As String
iCompare = vbBinaryCompare
StrTmpFile = App.Path & "\" & "SBC2DBC.tmp"
Open "C:\1.txt" For Input As #1
Open StrTmpFile For Output As #2
While Not EOF(1)
Line Input #1, Str1
Str2 = Str1
C1 = "A"
C2 = StrConv(C1, vbWide)
Str2 = Replace(Str2, C1, C2, , , iCompare)
Print #2, Str2
Wend
Close #1
Close #2
Kill "C:\1.txt"
Name StrTmpFile As "C:\1.txt"
End Sub
现测试文件:C:\1.txt的内容为:3841195078-87113-iMANHpBAcI
当把上述C:\1.txt放入上面字符串,存为Ansi编码时,运行上述程序后没有发现问题,A被成功替换成了“A”
当把上述C:\1.txt放入上面字符串,存为Unicode编码时,运行上述程序后,文件中的内容变成了:
?841195078-87113-iMANHpBAcI
前面的字符3变成了一个不可识别的符号加问号,而且用记事本查看,原来unicode编码的文件变成了Ansi编码。
请问,在VB6中,如何读写一个unicode编码的文本文件?
------解决方案--------------------
Ansi,Unicode(little endian),Unicode big endian,UTF-8编码文件相互转换
http://blog.****.net/chenjl1031/article/details/6059767
------解决方案--------------------
你用的vbWide,那是当然的。你看看我博客里面的是怎么用的。
------解决方案--------------------
把前两个字节去掉
------解决方案--------------------
- VB code
Private Sub cmdRead_Click() '读Unicode文本 Dim textBytes() As Byte, headBytes(2) As Byte fn = App.Path & "\MyUnicode.txt" If Dir(fn) = "" Then MsgBox "文件不存在,请先单击【写入】按钮生成“MyUnicode.txt”文件。", vbInformation Exit Sub End If Open fn For Binary As #1 Get #1, , headBytes() '读取文件头。 Close #1 If headBytes(0) = 255 And headBytes(1) = 254 Then '是Unicode编码。十六进制为:FF 、FE。 Open fn For Binary As #1 ReDim textBytes(LOF(1) - 2) '减去文件头占用的2个字节。 Get #1, 3, textBytes() '第三个字节起为文本内容。 Close #1 Text1.Text = textBytes() '在 VB 中字符串是 UniCode 格式,所以Unicode码直接赋值即可显示文本内容。 Else MsgBox "非Unicode编码,不予读入,请单击【写入】按钮。", vbInformation End If End Sub Private Sub cmdWrite_Click() '写Unicode文本。 Dim textBytes() As Byte, headBytes(2) As Byte headBytes(0) = 255: headBytes(1) = 254 textBytes() = Text2.Text Open App.Path & "\MyUnicode.txt" For Binary As #1 Put #1, 1, headBytes() ' 写文件头。 Put #1, 3, textBytes() ' 写文本内容。 Close #1 End Sub
------解决方案--------------------
- VB code
Private Sub Form_Load() Text2.Text = "3841195078-87113-iMANHpBAcI" End Sub
------解决方案--------------------
很简单啊,读取文件头,判断一下是否是Unicode编码.
------解决方案--------------------
推荐使用WinHex软件查看文件或内存中的原始字节内容。
------解决方案--------------------
Unicode little endian 开头2个字节是:FF FE
Unicode big endian 开头2个字节是:FE FF
------解决方案--------------------
详见字符编码:http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html
------解决方案--------------------