|
-
Jul 12th, 2017, 03:01 PM
#21
Thread Starter
Member
Re: Help for encoding a text file with VB6
 Originally Posted by dilettante
If your files are not extremely large you could use the ADO Stream class for this (ADODB.Stream):
Example:
Code:
Option Explicit
'Requires a reference to:
'
' Microsoft ActiveX Data Objects 2.5 Library (or later).
Private Sub Main()
Dim StreamIn As ADODB.Stream
Dim Header() As Byte
Dim HasBOM As Boolean
Dim StreamOut As ADODB.Stream
Set StreamIn = New ADODB.Stream
With StreamIn
'Load "utf-8.txt" UTF-8 file, detect BOM and skip over if present:
.Open
.Type = adTypeBinary
.LoadFromFile "utf-8.txt"
If .Size >= 3 Then
Header = .Read(3)
HasBOM = Header(0) = &HEF And Header(1) = &HBB And Header(2) = &HBF
End If
.Position = 0 'Allows us to change to text mode and assign Charset.
.Type = adTypeText
.Charset = "utf-8"
End With
Set StreamOut = New ADODB.Stream
With StreamOut
'Copy the text to "windows-1256.txt" ANSI 1252 file:
.Open
.Charset = "windows-1256"
.Type = adTypeText
'Move to beginning:
If HasBOM Then
StreamIn.Position = 3
Else
StreamIn.Position = 0
End If
.WriteText StreamIn.ReadText(adReadAll)
.SaveToFile "windows-1256.txt", adSaveCreateOverWrite
.Close
'Copy the text to "unicode.txt" UTF-16LE file:
.Open
.Charset = "unicode"
.Type = adTypeText
'Move to beginning:
If HasBOM Then
StreamIn.Position = 3
Else
StreamIn.Position = 0
End If
.WriteText StreamIn.ReadText(adReadAll)
.SaveToFile "unicode.txt", adSaveCreateOverWrite
.Close
End With
StreamIn.Close
MsgBox "Finished"
End Sub
Regarding the Charset property:
Thank you dear friend
My file is a movie subtitle file
You can further explain this class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|