Discussion:
Help: UTF-8 to ASCII
(too old to reply)
555 Rider
2005-05-19 04:23:39 UTC
Permalink
I need to read a text file which is UTF-8 Format and output to a standard
ASCII format in VB6. I found that the output file was stroed a lot of
incorrect character. How can I convert the character from UTF-8 to ASCII in
VB? I have tried read a text file use adodb object but the problem is the
same.

HELP!

Ron
Tony Proctor
2005-05-19 15:21:05 UTC
Permalink
Try some thing the following Ron:

Private Const CP_UTF8 = 65001

Private Declare Function MultiByteToWideChar Lib "kernel32" ( _
ByVal CodePage As Long, ByVal dwFlags As Long, _
ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, _
ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long

Public Function sUTF8ToUni(bySrc() As Byte) As String
' Converts a UTF-8 byte array to a Unicode string
Dim lBytes As Long, lNC As Long, lRet As Long

lBytes = UBound(bySrc) - LBound(bySrc) + 1
lNC = lBytes
sUTF8ToUni = String$(lNC, Chr(0))
lRet = MultiByteToWideChar(CP_UTF8, 0, VarPtr(bySrc(LBound(bySrc))),
lBytes, StrPtr(sUTF8ToUni), lNC)
sUTF8ToUni = Left$(sUTF8ToUni, lRet)
End Function

Public Sub ConvertUTF8File(sUTF8File As String, sANSIFile As String)
Dim iFile As Integer, bData() As Byte, sData As String, lSize As Long

' Get the incoming data size
lSize = FileLen(sUTF8File)
If lSize > 0 Then
ReDim bData(0 To lSize - 1)

' Read the existing UTF-8 file
iFile = FreeFile()
Open sUTF8File For Binary As #iFile
Get #iFile, , bData
Close #iFile

' Convert all the data to Unicode (all VB Strings are Unicode)
sData = sUTF8ToUni(bData)
Else
sData = ""
End If

' Now write it all out to the ANSI file
iFile = FreeFile()
Open sANSIFile For Output As #iFile
Print #iFile, sData
Close iFile
End Sub


Tony Proctor
Post by 555 Rider
I need to read a text file which is UTF-8 Format and output to a standard
ASCII format in VB6. I found that the output file was stroed a lot of
incorrect character. How can I convert the character from UTF-8 to ASCII in
VB? I have tried read a text file use adodb object but the problem is the
same.
HELP!
Ron
555 Rider
2005-05-20 01:31:06 UTC
Permalink
Its work! Thx your great Help!

Ron
Post by 555 Rider
I need to read a text file which is UTF-8 Format and output to a standard
ASCII format in VB6. I found that the output file was stroed a lot of
incorrect character. How can I convert the character from UTF-8 to ASCII in
VB? I have tried read a text file use adodb object but the problem is the
same.
HELP!
Ron
Loading...