Jefrey

ForceUTF8: forcedly convert strings to UTF-8 (without knowing its original encoding)

5 posts in this topic

#1 ·  Posted (edited)

Non-important short long story: I was facing several troubles when working with a webservice made in PHP and an AutoIt client. The AutoIt client had to send some data to the PHP webservice, then it would get back part of the data (with additional data) back to AutoIt, then send through Json to another webservice and end up in a TV system (huff). As I was using mostly Inet* functions, working with charsets became complicated, as it had to encode, decode, encode... and the mess is done. On the other end I was receiving a completely buggy string with several problems in accentuated characters (note that I live in Brazil, we speak portuguese that contains a lot of accented chars - Ááãç...). I tried and tried mixing utf_encodes and decodes everywhere, in PHP and Javascript. Wouldn't it be easier if I could just force the string to be UTF-8 and screw everything else? So I found toUTF8() PHP function.

I've ported toUTF8() function (truly, the whole Encoding class) by Sebastián Grignoli to AutoIt. It offers useful functions to force a string to be in a specified charset in a really easy way.

From the readme file:

Quote

 

AutoIt3 lacks of support to string encoding conversion and it tends to be painful when working with webservices and on-line APIs.

This lib addresses this issue and provides a handy function called toUTF8().

You don't need to know what is the encoding of your script. It can be Latin1 (ISO-8859-1), Windows-1252 OR UTF-8, or even have a mix of them. toUTF8() will convert everything to UTF-8.

There is also another function available, called fixUTF8(), which will fix the double (or multiple) encoded UTF-8 string that looks garbled.

 

Usage

$utf8_string = toUTF8($utf8_or_latin1_or_mixed_string)

$latin1_string = toLatin1($utf8_or_latin1_or_mixed_string)

Also:

$utf8_string = fixUTF8($garbled_utf8_string)

fixUTF8() converts the string to UTF-8 repeatedly until make sure it has only UTF-8 valid chars (it's really UTF-8).

Example:

#include 'forceutf8.au3'
MsgBox(0, '', fixUTF8( 'ãé' ) )

Will output:

ãé

Note that it's just a port. If you look at both the source codes together (PHP and AutoIt), you'll see that they're exactly the same thing, but in different approaches (PHP arrays converted to Scripting.Dictionary objects, function renames, syntax porting, a few functions completely rewritten due to differences between PHP and AutoIt). Therefore, all credits goes to Sebastián Grignol.

It seems that it works only with latin/roman alphabet (used by English).

Downloads

Download ZIP from Github

Wanna help?

Fork me on Github

Edited by Jefrey

My stuff

Spoiler

My UDFs  _AuThread multithreading emulation for AutoIt · _ExtInputBox an inputbox with multiple inputs and more features · forceUTF8 fix strings encoding without knowing its original charset · JSONgen JSON generator · _TCPServer UDF multi-client and multi-task (run on background) event-based TCP server easy to do · _TCPClient_UDF multi-server and multi-task (runs on background) event-based TCP client easy to do · ParseURL and ParseStr functions ported from PHP · _CmdLine UDF easily parse command line parameters, keys or flags · AutoPHP Create documents (bills, incomes) from HTML by sending variables/arrays from AutoIt to PHP · (Un)Serialize Convert arrays and data into a storable string (PHP compatible) · RTTL Plays and exports to MP3 Nokia-format monophonic ringtones (for very old cellphones) · I18n library Simple and easy to use localization library · Scripting.Dictionary OOP and OOP-like approach · Buffer/stack limit arrays to N items by removing the last one once the limit is reached · NGBioAPI UDF to work with Nitgen fingerprint readers · Serial/Licensing system require license key based on unique machine ID from your users · HTTP a simple WinHTTP library that allows GET, POST and file uploads · Thread true AutoIt threads (under-dev) · RC4 RC4 encryption compatible with PHP and JS Classes _WKHtmlToX uses wkhtmlto* to convert HTML files and webpages into PDF or images (jpg, bmp, gif, png...) Snippets _Word_DocFindReplaceByLongText replace strings using Word UDF with strings longer than 255 characters (MSWord limit) rangeparser parser for printing-like pages interval (e.g.: "1,2,3-5") EnvParser parse strings/paths with environment variables and get full path Random stuff Super Mario beep sound your ears will hurt

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Sorry but there are a number of misconception in what you write.

7 hours ago, Jefrey said:

MsgBox(0, '', fixUTF8( 'ãé' ) )

The string shown is nowhere UTF8. It is the UTF16 representation of the series of Latin1 bytes that encode ãé in UTF8.

Also, this part is wrong:

"You don't need to know what is the encoding of your script. It can be Latin1 (ISO-8859-1), Windows-1252 OR UTF-8, or even have a mix of them. toUTF8() will convert everything to UTF-8."

Of course you definitely need to know the source encoding, else you're going to corrupt it while converting it to UTF8. Mixed codepages can't work.

Finally, that is plain wrong as well:

" AutoIt3 lacks of support to string encoding conversion "

Just use _WinAPI_MultiByteToWideChar[Ex] and _WinAPI_WideCharToMultiByte wisely.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Well so let me explain them...

Quote

Sorry but there are a number of misconception in what you write.

On 19/06/2016 at 3:56 PM, Jefrey said:

MsgBox(0, '', fixUTF8( 'ãé' ) )

The string shown is nowhere UTF8. It is the UTF16 representation of the series of Latin1 bytes that encode ãé in UTF8.

Also, this part is wrong:

"You don't need to know what is the encoding of your script. It can be Latin1 (ISO-8859-1), Windows-1252 OR UTF-8, or even have a mix of them. toUTF8() will convert everything to UTF-8."

Of course you definitely need to know the source encoding, else you're going to corrupt it while converting it to UTF8. Mixed codepages can't work.

The purpose of this script (and the original popular PHP 'forceUTF8' where it was translated from) is to fix mixed-encoded strings, forcedly (dictionary-based), to UTF-8. It's really not needed to know the source encoding, as it uses a kind of table with common encoding errors and convert them kinda manually (said, if it finds "é", converts it directly to é). As it uses a dictionary with common char encoding errors, the string still can be a mix between multiple charsets.

Take a look on the original project in PHP (not mine): https://github.com/neitanod/forceutf8 and here is the main file

Quote

Finally, that is plain wrong as well:

" AutoIt3 lacks of support to string encoding conversion "

Just use _WinAPI_MultiByteToWideChar[Ex] and _WinAPI_WideCharToMultiByte wisely

The problem with the winapi (not exactly the AU3 lib) is that the function names aren't so clear... Is there some function to detect string encoding as well?

 


My stuff

Spoiler

My UDFs  _AuThread multithreading emulation for AutoIt · _ExtInputBox an inputbox with multiple inputs and more features · forceUTF8 fix strings encoding without knowing its original charset · JSONgen JSON generator · _TCPServer UDF multi-client and multi-task (run on background) event-based TCP server easy to do · _TCPClient_UDF multi-server and multi-task (runs on background) event-based TCP client easy to do · ParseURL and ParseStr functions ported from PHP · _CmdLine UDF easily parse command line parameters, keys or flags · AutoPHP Create documents (bills, incomes) from HTML by sending variables/arrays from AutoIt to PHP · (Un)Serialize Convert arrays and data into a storable string (PHP compatible) · RTTL Plays and exports to MP3 Nokia-format monophonic ringtones (for very old cellphones) · I18n library Simple and easy to use localization library · Scripting.Dictionary OOP and OOP-like approach · Buffer/stack limit arrays to N items by removing the last one once the limit is reached · NGBioAPI UDF to work with Nitgen fingerprint readers · Serial/Licensing system require license key based on unique machine ID from your users · HTTP a simple WinHTTP library that allows GET, POST and file uploads · Thread true AutoIt threads (under-dev) · RC4 RC4 encryption compatible with PHP and JS Classes _WKHtmlToX uses wkhtmlto* to convert HTML files and webpages into PDF or images (jpg, bmp, gif, png...) Snippets _Word_DocFindReplaceByLongText replace strings using Word UDF with strings longer than 255 characters (MSWord limit) rangeparser parser for printing-like pages interval (e.g.: "1,2,3-5") EnvParser parse strings/paths with environment variables and get full path Random stuff Super Mario beep sound your ears will hurt

 

Share this post


Link to post
Share on other sites
Quote

The purpose of this script (and the original popular PHP 'forceUTF8' where it was translated from) is to fix mixed-encoded strings, forcedly (dictionary-based), to UTF-8. It's really not needed to know the source encoding, as it uses a kind of table with common encoding errors and convert them kinda manually (said, if it finds "é", converts it directly to é). As it uses a dictionary with common char encoding errors, the string still can be a mix between multiple charsets.

This is simply impossible to do reliably.

Just take the simple example of yours: 'é' which your scipt will convert to 'é'. Let's imagine for simplicity that you actually have the following UTF8 byte sequence C3 A9. As a sidenote realize that this two byte sequence is completely different from the AutoIt string 'é' wich is represented in memory as C3 00 A9 00. But let's leave that detail burried.

Now please explain how you would sort out C3 A9 being the UTF8 sequence for é from any of the following distinct interpretations:

the Big5 ideogram (Unicode codepoint U+77C7 represented as E7 9F 87 in UTF8) or
the Euc-JP ideogram 辿 (Unicode codepoint U+8FBF represented as E8 BE BF in UTF8 ) or
the Shift-JIS ideograms テゥ (Unicode codepoints U+FF83 U+FF69 represented as EF BE 83 EF BD A9 in UTF8 ) or
the Euc-Kr (Unicode codepoint U+CC55 represented as EC B1 95 in UTF8 ) or?
just the characters é in Latin1 ( (Unicode codepoints U+00C3 U+00A9 represented as C3 83 C2 A9 in UTF8 ) or
dozens and dozens of distinct character(s), encoded is codepage(s) you ignore, all of these being represented by the very same C3 A9 sequence?

Answer: you can't.

All the different interpretations, being different characters, have a completely distinct UTF8 encoding, so what you propose is just guesswork. Even with single-byte characters > 0x7F in some codepage, you don't know which they represent as you have no idea which codepage they come from. The "dictionary with common char encoding errors" is just a pitfall waiting to trap unsuspecting users.

You may interpret sequences of bytes as UTF8, but don't pretend you can reliably convert any sequence of characters in unknown encodings (mixed or not), that's just plain impossible.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
Quote

the Big5 ideogram  (Unicode codepoint U+77C7 represented as E7 9F 87 in UTF8) or
the Euc-JP ideogram 辿 (Unicode codepoint U+8FBF represented as E8 BE BF in UTF8 ) or
the Shift-JIS ideograms テゥ (Unicode codepoints U+FF83 U+FF69 represented as EF BE 83 EF BD A9 in UTF8 ) or
the Euc-Kr  (Unicode codepoint U+CC55 represented as EC B1 95 in UTF8 ) or?
just the characters Ã© in Latin1 ( (Unicode codepoints U+00C3 U+00A9 represented as C3 83 C2 A9 in UTF8 ) or
dozens and dozens of distinct character(s), encoded is codepage(s) you ignore, all of these being represented by the very same C3 A9 sequence?

So, in fact, as I said on the OP, the lib isn't my work. It's already a solid work by neitanoid@github and I just converted it to AutoIt3, but it's exactly the very same code.

Although it's not mine (and I don't want to be devil's lawyer), it seems that the focus of the original project aren't chars out of roman/latin alphabet. Thus, the problem mentioned on the quoted part above will never happen as these chars aren't on it.

Note that as I'm not the original author of the work (PHP) and it's not mentioned on the project page (and I never faced the mentioned problem as I'm based in Brazil and we also use roman alphabet), I didn't know about this possible "focus" of the project.

Quote

Just take the simple example of yours: 'é' which your scipt will convert to 'é'. Let's imagine for simplicity that you actually have the following UTF8 byte sequence C3 A9. As a sidenote realize that this two byte sequence is completely different from the AutoIt string 'é' wich is represented in memory as C3 00 A9 00. But let's leave that detail burried.

In my tests, this is more related to the .au3 file charset than to an AutoIt encoding.

Quote

The "dictionary with common char encoding errors" is just a pitfall waiting to trap unsuspecting users.

Users following the recomendation above (adding to the OP) have nothing to fear.

 

Also, I'd suggest you to fill an issue up with these problems found, although I think they will give the same answer: https://github.com/neitanod/forceutf8/issues/new


My stuff

Spoiler

My UDFs  _AuThread multithreading emulation for AutoIt · _ExtInputBox an inputbox with multiple inputs and more features · forceUTF8 fix strings encoding without knowing its original charset · JSONgen JSON generator · _TCPServer UDF multi-client and multi-task (run on background) event-based TCP server easy to do · _TCPClient_UDF multi-server and multi-task (runs on background) event-based TCP client easy to do · ParseURL and ParseStr functions ported from PHP · _CmdLine UDF easily parse command line parameters, keys or flags · AutoPHP Create documents (bills, incomes) from HTML by sending variables/arrays from AutoIt to PHP · (Un)Serialize Convert arrays and data into a storable string (PHP compatible) · RTTL Plays and exports to MP3 Nokia-format monophonic ringtones (for very old cellphones) · I18n library Simple and easy to use localization library · Scripting.Dictionary OOP and OOP-like approach · Buffer/stack limit arrays to N items by removing the last one once the limit is reached · NGBioAPI UDF to work with Nitgen fingerprint readers · Serial/Licensing system require license key based on unique machine ID from your users · HTTP a simple WinHTTP library that allows GET, POST and file uploads · Thread true AutoIt threads (under-dev) · RC4 RC4 encryption compatible with PHP and JS Classes _WKHtmlToX uses wkhtmlto* to convert HTML files and webpages into PDF or images (jpg, bmp, gif, png...) Snippets _Word_DocFindReplaceByLongText replace strings using Word UDF with strings longer than 255 characters (MSWord limit) rangeparser parser for printing-like pages interval (e.g.: "1,2,3-5") EnvParser parse strings/paths with environment variables and get full path Random stuff Super Mario beep sound your ears will hurt

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • rootx
      By rootx
      I need help with unicode char ü I get some text from online json but if try to read 4 example Zürich I heave  Zürich.
      How can I convert with autoit unicode to a clear character readable? thx
    • ur
      By ur
      I have recorded a gui installation and modified the au3 file as per my requirement.
      But the problem is, whenever I try to compile the au3 file, I am getting a special character on top of the file.

      Can you suggest how to resolve this as it is appearing everytime during compilation.
      And also I am trying to include a au3 file which is separate folder so I am adding the <..\Library.au3> line.
      It is able to pick that but in that file "Library.au3" there is a file install line.
      FileInstall(".\SendMail.ps1", @ScriptDir & "\SendMail.ps1") Library.au3 is being used by many other codes in the previous folder..
      But the current file is looking for the ps1 file in current folder.
      So is there any option to tell the compiler to pick the dependent files relative to Library.au3 instead of the current code.
    • MattG1991
      By MattG1991
      Hello all, 
      I've written a script that pulls info from some web pages, saves the results to variables, and prints it out to a Word document. I've tried to make this script work with a minimum of user interaction. I've noticed an issue happens when I run the script and I have another window set as active, like SciTE for example. It inserts an "ETX" character. I've done this with Notepad too. It displays an upside down L character. Any particular reason why? Unfortunately, I can't post any code. Basically, it gets a collection of tags, applies a test to find the values I want, then pulls the innerhtml from said tag. Then it assigns the results to a global variable.  Any help would be appreciated. 
    • legend
      By legend
      Hello
      I'm using the code below to send mails using our internal relay server. 
      We got a Helpdesk system named Remedy. Our users can send us a mails using outlook 2010, and we'll get a ticket.
      The problem is if I send a mail using the above script our ticket system can't display unicode characters, like ex: Æ Ø Å. It will display them as: questionmarks: "? ? ?" inside our ticket system. In the outlook inbox it looks fine showing unicode symbols, but in our ticket system the unicode characters will be replaced by questionmarks.
      The thing is, if they send a mail using outlook, it works fine, but using the script above it doesen't.
      I tried to save my script with encoding: UTF-8 with BOM, but it didn't fix it.
       
      All suggestions are very welcome
       
       
       
       
    • Xenobiologist
      By Xenobiologist
      Hi folks,
      I'm using the udf #include <FTPEx.au3> to "download" some files from our zOS (MVS) system via autoit FTP with _FTP_FileGet.
      The FileGet works, but the codepage is wrong. Special German chars like öäüß are not transfered correctly.
      Any idea how to set the codepage? Swichting between binary or ascii doesn't solve the problem.
      Using the ftp command in a cmd.exe window it works like this : quote site sbd=(IBM-273,iso8859-1)
      Thanks
      Mega