How Do I Convert a Unicode BLOB to a Binary BLOB with UTF-8 Content? (Magic xpi 4.9)
Magic xpi lets you easily convert a Unicode BLOB to a binary BLOB with UTF-8 content. You do this with the UTF8fromUnicode function.
This topic makes use of the utf16_output_valid.txt Unicode BLOB that was created in the How Do I Convert a Binary BLOB with UTF-8 Content to a Unicode BLOB? topic. If you create a new project for this current topic, you should copy the utf16_output_valid.txt file over to the new project’s <My Documents>\Magic\projects\ folder.
If you did not previously work with the How Do I Convert a Binary BLOB with UTF-8 Content to a Unicode BLOB? topic, you should create your own Unicode BLOB file to work with this example and save it under your <My Documents>\Magic\projects\<current project> folder. Call this file utf16_output1.txt.
Converting a Unicode BLOB to a binary BLOB with UTF-8 content using the UTF8fromUnicode function:
-
To create the variables that you will use in this example, open the Flow Variables repository.
-
There, create two BLOB variables called F.utf8 and F.utf16. Then, create a logical variable called F.log.
-
Drag a Flow Data utility into your flow.
-
To begin the conversion process, you need to load the Unicode BLOB into a file using the File2Blb function. In the Flow Data Configuration dialog box, click Add.
-
In the Action column, select Update from the drop-down list.
-
In the Type column, select Flow.
-
In the Name column, select the F.utf16 BLOB variable that you previously created.
-
In the Encoding column, select Unicode.
-
In the Update Expression column, click to open the Expression Editor.
-
In the Expression Editor, enter the following expression (as shown in the image below), and then click OK:
File2Blb (EnvVal ('currentprojectdir')&'utf16_output1.txt')
The next stage is to convert the utf16_output1.txt file to a UTF-8 BLOB variable using the UTF8fromUnicode function.
-
In the Flow Data Configuration dialog box, click Add.
-
In the Action column, select Update from the drop-down list.
-
In the Type column, select Flow.
-
In the Name column, select the F.utf8 BLOB variable that you previously created.
-
In the Encoding column, select Binary.
-
In the Update Expression column, click to open the Expression Editor.
-
In the Expression Editor, enter the following expression (as shown in the image below), and then click OK:
UTF8fromUnicode ( F.utf16 )
Next, you need to write the UTF-8 BLOB variable to the file system.
-
In the Flow Data Configuration dialog box, click Add.
-
In the Action column, select Update from the drop-down list.
-
In the Type column, select Flow.
-
In the Name column, select the F.log logical variable that you previously created.
-
In the Update Expression column, click to open the Expression Editor.
-
In the Expression Editor, enter the following expression (as shown in the image below), and then click OK:
Blb2File ( F.utf8 , EnvVal('currentprojectdir') &'utf8_output.txt')
Now, run the project to see what is created in the utf8_output.txt file that you defined in step 6 (above).
-
Click OK to close the Flow Data Configuration dialog box.
-
Build your project.
-
Open the Magic xpi Monitor, and start the project.
-
Open the<My Documents>\Magic\projects\<current project> folder. You will see the newly-created utf8_output.txt file.
When you open the utf8_output.txt file, its contents are correctly encoded in UTF-8. If you check the contents using a Hex Editor, you will see that the file does not contain a BOM.
The next stage explains how to add a BOM to BLOB variables.
-
In the Flow Data Configuration dialog box, click Add.
-
In the Action column, select Update from the drop-down list.
-
In the Type column, select Flow.
-
In the Name column, select the F.utf8 BLOB variable.
-
In the Encoding column, select Binary.
-
In the Update Expression column, click to open the Expression Editor.
-
In the Expression Editor, enter the following expression (as shown in the image below), and then click OK:
BlobAddBOM ( F.utf8 ,1)
-
Use the up arrow on the right hand side of the Flow Data Configuration dialog box to move this new entry up to the third line of the table.
-
Delete the existing utf8_output.txt file from the <My Documents>\Magic\projects\<current project> folder.
Now, run the project again to see what is created this time in the utf8_output.txt file.
-
Click OK to close the Flow Data Configuration dialog box.
-
Build your project.
-
Open the Magic xpi Monitor, and start the project.
-
Open the <My Documents>\Magic\projects\<current project> folder. You will see the updated utf8_output.txt file.
When you open the utf8_output.txt file, its contents are correctly encoded in UTF-8. Not only that, if you check the contents using a Hex Editor, you will see that the file also contains a BOM.
How Do I Convert a Binary BLOB with UTF-8 Content to a Unicode BLOB?