Tech Tip: Getting Unicode text into a Non-Unicode Database
PRODUCT: 4D | VERSION: 11 | PLATFORM: Mac & Win
Published On: September 2, 2010
4D v11 SQL introduced the Unicode character set as the default in 4D. But you can still run a converted database in Non-unicode mode, which can cause problems when you are trying to access Unicode data. You can use the functionality of 4D Components and new commands in 4D v11 SQL to get around this.
For example you want to import the following text, encoded as UTF-8:
Jacques Fréchon Thomas Keßler |
But when you try to import you end up with bad characters. Something like:
Jacques Fr?chon Thomas Ke?ler |
The "?" characters above could come out as any different variation of bad characters.
First create a component with one method. Insert the following code in that method and check its properties as "Shared by components and host database".
` convert_text method ` pass in a blob of Unicode text to convert ` return value is a blob of text in MacRoman C_BLOB($1;$myblob) C_TEXT($text) If (Count parameters=1) $myblob:=$1 $text:=Convert to text($myblob;"UTF-8") CONVERT FROM TEXT($text;"MacRoman";$myblob) Else SET BLOB SIZE($myblob;0) End if $0:=$myblob |
This database is a Unicode database and therefore can use Convert to text to get the Unicode text. Then the method changes it to the MacRoman character set before putting in the return value BLOB.
Install the Component database into the host database. Now you can access the convert_text method in your Non-unicode host database. This can be done with the following code:
C_TIME($doc) C_BLOB($myblob;$returnVal) C_TEXT($textVal) $doc:=Open document("") ` Upload your UTF-8 document here CLOSE DOCUMENT($doc) DOCUMENT TO BLOB(Document;$myblob) EXECUTE METHOD("convert_text";$returnVal;$myblob) $textVal:=BLOB to text($returnVal;Mac text without length ) |
Note that the EXECUTE METHOD command is used to call the convert_text method. In this way a Non-unicode database can call a Unicode Mode Component method. The $textVal variable at the end will have "good" data, from the previous example it would be:
Jacques Fréchon Thomas Keßler |