Ansi,unicode (little endian), Unicode big endian,utf-8 encoded files convert each other __ encoding

Source: Internet
Author: User
Tags save file trim

Option Explicit Private Declare Function multibytetowidechar Lib "Kernel32.dll" (ByVal CodePage as Long, ByVal DwFlags as Long, ByVal lpmultibytestr as String, ByVal cchmultibyte as Long, ByVal lpwidecharstr as String, ByVal Cchwidechar as Long As Long Private Declare Function widechartomultibyte Lib "Kernel32.dll" (ByVal CodePage as Long, ByVal dwFlags as Long, ByVal Lpwidecharstr as Long, ByVal Cchwidechar as Long, ByRef lpmultibytestr as any, ByVal cchmultibyte as Long, ByVal LpD Efaultchar as String, ByVal Lpuseddefaultchar as Long) as long Private Const CP_ACP As Long = 0 Private Const CP_UTF8 as L ONG = 65001 ' ansi plain text file converted to Unicode (Little Endian) text file Private Function ansitoule (ByVal inputansifile as String, ByVal Output Ulefile As String) as Boolean Dim filebyte () as Byte, Sansi as String, Retlen as Long, filenumber as Long Dim Sunicodebuff Er as String on Error Resume Next ' Open ANSI plain text file inputansifile filenumber = FreeFile If Dir (inputansifile) = "Then Ansitoul E = False:exit Function Open inputansifile for Binary as #FileNumber ReDim filebyte (LOF (filenumber)-1) Get #FileNumber, Filebyte Close #FileN Umber Sansi = StrConv (filebyte, Vbunicode) ' Convert to VB6 can display string retlen = MultiByteToWideChar (CP_ACP, 0, Sansi, LenB (Sansi), VBNU Llchar, 0) ' Required space size after conversion retlen Sunicodebuffer = string$ (LenB (Sansi), vbNullChar) ' Set buffer size If retlen > 0 Then Retlen = M Ultibytetowidechar (CP_ACP, 0, Sansi, LenB (Sansi), Sunicodebuffer, Retlen) ' Start conversion Else ansitoule = False:exit Function End If ' Save as Unicode (Little Endian) text file outputulefile if Retlen > 0 Then filenumber = FreeFile if Dir (outputulefile) <> "Then Kill (outputulefile) Open outputulefile for Binary as #FileNumber Put #FileNumber, &hfeff ' plus Unicode (Little Endian) file header BOM flag Fffe Put #FileNumber, sunicodebuffer ' save file contents Close #FileNumber ansitoule = True Else ansitoule = false:e xit function End If End Function ' ANSI plain text file converted to Unicode Big endian text file Private Function ansitoube (ByVal inputansifile as Str ING, ByVal outputubefile As String) as Boolean Dim filebyte () As Byte, Fbyte () As Byte Dim Sansi As String, Retlen as Long, filenumber as Long Dim Sunicodebuffer as String Dim i as Long on Error Resume Next ' Open ANSI plain text file inputansifile filenumber = FreeFile If Dir (inputa Nsifile) = "then Ansitoube = false:exit Function Open inputansifile for Binary as #FileNumber ReDim filebyte (LOF (filenu mber)-1) Get #FileNumber, filebyte Close #FileNumber sansi = StrConv (filebyte, Vbunicode) ' Convert to VB6 can display string retlen = Mult Ibytetowidechar (CP_ACP, 0, Sansi, LenB (Sansi), vbNullChar, 0) ' takes the space size after conversion retlen Sunicodebuffer = string$ (LenB (Sansi), vbNullChar) ' Set buffer size If retlen > 0 Then Retlen = MultiByteToWideChar (CP_ACP, 0, Sansi, LenB (Sansi), Sunicodebuffer, ret Len) ' Start convert Else ansitoube = false:exit Function End if ' Save as Unicode Big endian text file outputubefile If retlen > 0 Then Redi M Filebyte (LenB (Sansi)-1), Fbyte (LenB (Sansi)-1) filebyte = StrConv (Sunicodebuffer, vbfromunicode) for i = 0 to UBound ( Filebyte) If I Mod 2 = 0 THen Fbyte (i) = Filebyte (i + 1) Else fbyte (i) = Filebyte (i-1) End if Next filenumber = FreeFile if Dir (outputubefile) &lt ;> "then Kill (outputubefile) Open outputubefile for Binary as #FileNumber Put #FileNumber, &hfffe ' plus Unicode (B IG Endian) file header BOM flag Feff Put #FileNumber, fbyte ' sunicodebuffer ' save file contents Close #FileNumber ansitoube = True Else ansitoube = False:exit function End If End Function ' ANSI plain text file converted to UTF-8 text file Private Function AnsiToUTF8 (ByVal inputansifile as Stri Ng, ByVal Outpututf8file As String) as Boolean Dim filebyte () As Byte ', Fbyte () as Byte Dim Sansi as String, Retlen as Lo Ng, filenumber as Long Dim Sutf8buffer () as Byte, S as String on Error Resume Next ' Open ANSI plain text file Inputansifile filenumber = FreeFile If Dir (inputansifile) = "" Then AnsiToUTF8 = false:exit Function Open inputansifile for Binary as #FileNumber Re Dim Filebyte (LOF (filenumber)-1) Get #FileNumber, filebyte Close #FileNumber S = Filebyte Sansi = StrConv (S, Vbunicode) ' Convert to VB6 to display the string Retlen = WideCharToMultiByte (Cp_utf8, 0, StrPtr (sansi),-1, vbNullString, 0, vbNullString, 0) ' Get converted space required after Retlen If retlen > 0 Then ReDim sutf8buffer (retLen-1) ' = string$ (Retlen, vbNullChar) ' Set buffer size Retlen = WideCharToMultiByte (Cp_utf8, 0, STRP TR (Sansi),-1, sutf8buffer (0), Retlen, vbNullString, 0) ' start conversion Else AnsiToUTF8 = false:exit Function End If ' Save as UTF-8 text file o Utpututf8file If retlen > 0 Then ReDim Preserve sutf8buffer (retLen-1) S = StrConv (Sutf8buffer, Vbunicode) filenumber = FreeFile If Dir (outpututf8file) <> ' then Kill ' (outpututf8file) Open outpututf8file for Binary as #FileNumber Put #FileNumber, &hbfbbef ' plus UTF-8 file header BOM Mark EFBBBF Put #FileNumber, 4, S ' Save file contents Close #FileNumber AnsiToUTF8 = True Else AnsiToUTF8 = False:exit function End If End Function ' UTF-8 text file converted to Unicode (Little Endian) text file Private Function utf8toule ( ByVal Inpututf8file As String, ByVal Outputulefile as String) as Boolean Dim filebyte () As Byte ', Fbyte () As Byte Dim sAn Si as String, retlen asLong, filenumber as Long Dim Sutf8buffer as String, S as String on Error Resume Next ' open UTF-8 text file Inpututf8file filenumber = FreeFile If Dir (inpututf8file) = "" Then Utf8toule = false:exit Function Open inpututf8file for Binary as #FileNumber R Edim Filebyte (LOF (filenumber)-1) Get #FileNumber, filebyte Close #FileNumber If hex$ (filebyte (0)) = "EF" and hex$ (File BYTE (1)) = "BB" and hex$ (Filebyte (2)) = "BF" then S = Filebyte Else MsgBox (Inpututf8file & "for non-UTF-8 encoded format file!") Utf8toule = false:exit Function End If sansi = StrConv (S, Vbunicode) ' Convert to VB6 can display the string Retlen = MultiByteToWideChar (Cp_utf8, 0, Sansi,-1, vbNullChar, 0) ' Required space size after conversion Retlen If retlen > 0 Then Sutf8buffer = string$ (Retlen * 2, vbNullChar) ' Set slow Flush size Retlen = MultiByteToWideChar (Cp_utf8, 0, Sansi,-1, Sutf8buffer, Retlen * 2) ' start conversion Else utf8toule = False:exit funct Ion End if ' saved as Unicode (Little Endian) text file outputulefile If retlen > 0 Then S = left$ (Sutf8buffer, Retlen * 2) filenumber = FreeFile If Dir (outputulefile) <> "then Kill (outputulefile) Open outputulefile for Binary as #FileNumber Put #FileNumber, S ' Save the file contents, the program automatically adds Unicode (Little Endian) file header BOM flag Fffe Close #FileNumber utf8toule = True Else utf8toule = false:exit Function End If End Fun Ction ' UTF-8 text file converted to Unicode (Big Endian) text file Private Function utf8toube (ByVal inpututf8file as String, ByVal outputubefile As String) as Boolean Dim filebyte () As Byte, Fbyte () As Byte Dim Sansi As String, Retlen as Long, filenumber as Long Dim Sutf8buffer As String, S as String Dim I as Long on Error Resume Next ' open UTF-8 text file inpututf8file filenumber = FreeFile If D IR (inpututf8file) = "then Utf8toube = false:exit Function Open inpututf8file for Binary as #FileNumber ReDim filebyte (L Of (FileNumber)-1) Get #FileNumber, filebyte Close #FileNumber If hex$ (filebyte (0)) = "EF" and hex$ (Filebyte (1)) = "BB" and hex$ (Filebyte (2)) = "BF" then S = Filebyte Else MsgBox (Inpututf8file & "for non-UTF-8 encoded format file!") Utf8toube = false:exit Function End If Sansi = StrConv (S, Vbunicode) ' converted to VB6 can display the string Retlen = MultiByteToWideChar (Cp_utf8, 0, Sansi,-1, vbNullString, 0) ' Required space size after conversion ' re TLen If retlen > 0 Then Sutf8buffer = string$ (Retlen * 2, vbNullChar) ' Set buffer size Retlen = MultiByteToWideChar (Cp_utf8, 0, Sansi,-1, Sutf8buffer, Retlen * 2) ' start convert Else utf8toube = false:exit Function End If ' Save as Unicode Big endian text file outputub Efile If retlen > 0 Then ReDim filebyte (LenB (Sansi)-1), Fbyte (LenB (Sansi)-1) filebyte = StrConv (left$ (Sutf8buffer, Retlen * 2), vbfromunicode) for i = 0 to UBound (filebyte) If i Mod 2 = 0 Then fbyte (i) = Filebyte (i + 1) Else fbyte (i) = F Ilebyte (i-1) End If Next filenumber = FreeFile If Dir (outputubefile) <> "then Kill (outputubefile) Open outputub Efile for Binary as #FileNumber Put #FileNumber, Fbyte ' saves the contents of the file, the program automatically adds the Unicode (Big Endian) header BOM Mark Feff Close #FileNumber Utf8toube = True Else Utf8toube = false:exit function End If End Function ' UTF-8 text file converted to ANSI plain text file Private Function Utf8toa NSI (ByVal Inpututf8fileAs String, ByVal Outputansifile as String) as Boolean Dim filebyte () As Byte ', Fbyte () as Byte Dim Sansi as String, Retle N as long, filenumber as Long Dim Sutf8buffer as String, S as String ' Dim I as Long ' on Error Resume Next ' Open UTF-8 text file input Utf8file filenumber = FreeFile If Dir (inpututf8file) = "then Utf8toansi = false:exit Function Open inpututf8file for Bi Nary as #FileNumber ReDim filebyte (LOF (filenumber)-1) Get #FileNumber, filebyte Close #FileNumber If hex$ (filebyte (0)) = "EF" and hex$ (Filebyte (1)) = "BB" and hex$ (Filebyte (2)) = "BF" then S = Filebyte Else MsgBox (Inpututf8file & "for non u TF-8 encoded format file! ") Utf8toansi = false:exit Function End If sansi = StrConv (S, Vbunicode) ' Convert to VB6 can display string retlen = MultiByteToWideChar (Cp_utf8 , 0, Sansi,-1, vbNullString, 0) ' Required space size after conversion Retlen If retlen > 0 Then Sutf8buffer = string$ (Retlen * 2, vbNullChar) ' Set Buffer Size Retlen = MultiByteToWideChar (Cp_utf8, 0, Sansi,-1, Sutf8buffer, Retlen * 2) ' start conversion Else Utf8toansi = False:exit F Unction EnD if ' Save as ANSI plain text file Outputansifile If retlen > 0 Then s = left$ (Sutf8buffer, Retlen * 2) s = StrConv (S, vbFromUnicode) Mi d$ (S, 1, 1) = "": s = Trim (s) filenumber = FreeFile If Dir (outputansifile) <> "then Kill (outputansifile) Open Ou Tputansifile for Binary as #FileNumber Put #FileNumber, S ' save file contents Close #FileNumber utf8toansi = True Else Utf8toansi = False:exit function End If End Function ' Unicode (Little Endian) text file converted to ANSI plain text file Private Function uletoansi (ByVal inputule File As String, ByVal Outputansifile as String) as Boolean Dim filebyte () As Byte ', Fbyte () as Byte Dim Sansi as String, Retlen as Long, filenumber as Long Dim sunicodebuffer () as Byte, S as String ' Dim I as Long ' on Error Resume Next ' Open unicod E (Little Endian) text file inputulefile filenumber = FreeFile If Dir (inputulefile) = "then Uletoansi = False:exit Function Ope n inputulefile for Binary as #FileNumber ReDim filebyte (LOF (filenumber)-1) Get #FileNumber, Filebyte Close #FileNumber If hex$ (filebyte (0)) ="FF" and hex$ (Filebyte (1)) = "FE" then S = Filebyte Else MsgBox (inputulefile & "encoded format file for non-Unicode (Little Endian)!") Uletoansi = false:exit Function end If Sansi = StrConv (S, Vbnarrow) ' Convert to VB6 can display string ' to this place, should say can end, VB6 convert with StrConv, directly Sansi The file can be saved ' below is the API converted to ANSI code Sansi = S Retlen = WideCharToMultiByte (CP_ACP, 0, StrPtr (sansi), 1, vbNullString, 0, Vbnullstrin G, 0) ' The space size required after conversion Retlen If retlen > 0 Then ReDim sunicodebuffer (Retlen * 2-1) ' string$ (Retlen * 2, vbNullChar) ' Set slow Flushing size Retlen = WideCharToMultiByte (CP_ACP, 0, StrPtr (sansi),-1, sunicodebuffer (0), Retlen * 2, vbNullString, 0) ' Start converting Else Uletoansi = false:exit Function End if ' Save as ANSI plain text file Outputansifile If retlen > 0 Then ReDim Preserve sunicodebuffer (r etLen-1) s = StrConv (Sunicodebuffer, Vbunicode) mid$ (S, 1, 1) = "": s = Trim (s) filenumber = FreeFile If Dir (outputansi File) <> "then Kill (outputansifile) Open outputansifile for Binary as #FileNumber Put #FileNumber, S ' Save file contents Cl OSE #FileNumber Uletoansi= True Else Uletoansi = false:exit function End If End Function ' Unicode (Little Endian) text file converted to Unicode Big Endian text file. The ' Unicode Big Endian text file is converted to a Unicode (Little Endian) text file, ' only hex$ (filebyte (0)) = "FF" and hex$ (Filebyte (1)) = "FE" is changed to ' hex$ ' (F Ilebyte (0)) = "FE" and hex$ (Filebyte (1)) = "FF". Private Function Uletoube (ByVal inputulefile As String, ByVal Outputubefile as String) as Boolean Dim Filebyte () as Byte, Fbyte () As Byte ' Dim sansi As String, Retlen as Long ' dim Sunicodebuffer () As Byte, S As String Dim i as Long, filenumber As Long on Error Resume Next ' Open Unicode (Little Endian) text file inputulefile filenumber = FreeFile If Dir (inputulefile) = "" The n uletoube = false:exit Function Open inputulefile for Binary as #FileNumber ReDim filebyte (LOF (filenumber)-1), Fbyte (L Of (FileNumber)-1) Get #FileNumber, filebyte Close #FileNumber If hex$ (filebyte (0)) = "FF" and hex$ (Filebyte (1)) = "FE" Then ' Unicode (Little Endian) encoded format file Else MsgBox (Inputulefile & "for non-Unicode (Little Endian) encoded format textPieces! ") Uletoube = False:exit Function End if for me = 0 to UBound (filebyte) If I Mod 2 = 0 Then fbyte (i) = Filebyte (i + 1) Else F BYTE (i) = Filebyte (i-1) End if Next ' Save as Unicode Big endian text file Outputubefile filenumber = FreeFile if Dir (outputubefile) <> "then Kill (outputubefile) Open outputubefile for Binary as #FileNumber Put #FileNumber, fbyte ' Save file contents Close #FileNumber End function ' Unicode (Little Endian) text file converted to UTF-8 text file Private Function ULEToUTF8 (ByVal inputulefile as String , ByVal Outpututf8file As String) as Boolean Dim filebyte () As Byte ', Fbyte () as Byte Dim Sansi as String, Retlen as Long , filenumber as Long Dim Sutf8buffer () as Byte, S as String on Error Resume Next ' Open Unicode (Little Endian) text file Inputulefile FileNumber = FreeFile If Dir (inputulefile) = "then ULEToUTF8 = false:exit Function Open inputulefile for Binary as #Fi Lenumber ReDim filebyte (LOF (filenumber)-1) Get #FileNumber, filebyte Close #FileNumber If hex$ (filebyte (0)) = "FF" and hex$ (Filebyte (1)) ="FE" then S = Filebyte Else MsgBox (Inputulefile & "for non-Unicode (Little Endian) encoded format file!") ULEToUTF8 = false:exit Function End If sansi = StrConv (S, Vbnarrow) ' converted to VB6-displayed string mid$ (Sansi, 1, 1) = "": Sansi = Trim ( Sansi) Retlen = WideCharToMultiByte (Cp_utf8, 0, StrPtr (sansi), 1, vbNullString, 0, vbNullString, 0) ' space required after conversion Retlen I F retlen > 0 then ReDim sutf8buffer (retLen-1) ' = string$ (Retlen, vbNullChar) ' Set buffer size Retlen = WideCharToMultiByte (C P_utf8, 0, StrPtr (sansi),-1, sutf8buffer (0), Retlen, vbNullString, 0) ' start conversion Else ULEToUTF8 = false:exit Function End If ' Save as UTF-8 text file Outpututf8file If retlen > 0 Then ReDim Preserve sutf8buffer (retLen-1) S = StrConv (Sutf8buffer, Vbunicod e) filenumber = FreeFile If Dir (outpututf8file) <> "then Kill (outpututf8file) Open outpututf8file for Binary as # FileNumber put #FileNumber, &hbfbbef ' plus UTF-8 file header BOM flag efbbbf Put #FileNumber, 4, S ' Save file contents Close #FileNumber Uletoutf 8 = True Else ULEToUTF8 = false:exit Function End If End Function Private Sub command1_click () ' Create a new ANSI plain text file ' d:/ansicodefile.txt ' ANSI plain text file converted to a Unicode (Little Endian) text file Call Ansitoule ("D:/ansicodefile.txt", "d:/ansitounicodelefile.txt") ' ANSI plain text file converted to Unicode (Big Endian) text file call Ansitoube ("D:/ansicodefile.txt", "d:/ansitounicodebefile.txt") ' ANSI plain text file converted to UTF-8 text file call AnsiToUTF8 ("d:/ AnsiCodeFile.txt "," d:/ansitoutf8file.txt ") ' UTF-8 text file converted to Unicode (Little Endian) text file call Utf8toule (" d:/ AnsiToUTF8File.txt "," d:/utf8tounicodelefile.txt ") ' UTF-8 text file converted to Unicode Big endian text file call Utf8toube (" d:/ AnsiToUTF8File.txt "," d:/utf8tounicodebefile.txt ") ' UTF-8 text file converted to ANSI plain text file call Utf8toansi (" D:/ansitoutf8file.txt ", "D:/utf8toansifile.txt") ' Unicode (Little Endian) text file converted to ANSI plain text file call Uletoansi ("D:/ansitounicodelefile.txt", "d:/ UnicodeLEToAnsiFile.txt ") ' Unicode (Little Endian) text file converted to Unicode Big Endian text file call Uletoube (" d:/ AnsiToUnicodeLEFile.txt "," d:/unicodeletounicodebefile.txt ") ' Unicode (Little Endian) text file converted to UTF-8 text file call ULEToUTF8 ("D:/ansitounicoDeLEFile.txt "," D:/unicodeletoutf8file.txt ") End Sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.