Example: /Util/Fix bad encoding

Online Documentation   -   Statistics   -   FAQ   -   Plugin Parts (All, Dependencies)   -   Class hierarchy

New in Version 22.2 22.3 22.4 22.5 23.0 23.1 23.2 23.3 23.4 23.5 24.0 24.1

The list of the   topics,   classes,   interfaces,   controls,   modules,   global methods by category,   global methods by name,   screenshots,   licenses   and   examples.

Platforms to show: All Mac Windows Linux Cross-Platform

/Util/Fix bad encoding


Required plugins for this example:

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Util/Fix bad encoding

This example is the version from Wed, 4th Dec 2018.

Project "Fix bad encoding.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
Function FixEncoding(s as string) As string dim UTF8 as TextEncoding = encodings.UTF8 if not UTF8.IsValidData(s) then break // this function works only on UTF-8 text! return s end if // how many ? do we have? dim CountFieldsS as Integer = CountFieldsB(s, "?") dim u as integer = encodings.Count-1 for i as integer = 0 to u // we try each encoding dim e as TextEncoding = encodings.Item(i) dim internetName as string = e.internetName if internetName.left(3) <> "UTF" then // skip all UTF variants // now convert back to old encoding dim t as string = ConvertEncoding(s, e) if UTF8.IsValidData(t) then // looks like the new text is valid UTF-8 dim CountFieldsT as integer = CountFieldsB(t, "?") if CountFieldsT = CountFieldsS then // and conversion didn't find characters it didn't like dim r as string = DefineEncoding(t, encodings.UTF8) // we got a fixed string return r end if end if end if next End Function
Sub Test() dim SomeText as string = "Vielen Dank für ihr Verständnis." // lets say you would save this text in other encoding dim SomeText_WindowsANSI as string = DefineEncoding(SomeText, encodings.WindowsANSI) dim SomeText_MacRoman as string = DefineEncoding(SomeText, encodings.MacRoman) dim SomeText_DOSLatin1 as string = DefineEncoding(SomeText, encodings.DOSLatin1) // and now get that as UTF-8 dim SomeText_WindowsANSI_UTF8 as string = ConvertEncoding(SomeText_WindowsANSI, Encodings.UTF8) dim SomeText_MacRoman_UTF8 as string = ConvertEncoding(SomeText_MacRoman, Encodings.UTF8) dim SomeText_DOSLatin1_UTF8 as string = ConvertEncoding(SomeText_DOSLatin1, Encodings.UTF8) // now you have the broken text you don't want. // e.g. Vielen Dank für ihr Verständnis. // now we want to fix. dim SomeText_WindowsANSI_UTF8_fixed as string = FixEncoding(SomeText_WindowsANSI_UTF8) dim SomeText_MacRoman_UTF8_fixed as string = FixEncoding(SomeText_MacRoman_UTF8) dim SomeText_DOSLatin1_UTF8_fixed as string = FixEncoding(SomeText_DOSLatin1_UTF8) // and you see in debugger all texts are back right Break End Sub
End Class
Class Window1 Inherits Window
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
End Project

The items on this page are in the following plugins: MBS Util Plugin.


💬 Ask a question or report a problem