How Do I Add a Unicode or a UTF-8 BOM to a BLOB File? (Magic xpi 4.1)
Byte order marks (BOMs) are used to indicate the byte order of a text file. There might be occasions that you want to add a BOM to a BLOB file to help you with encoding detection. You can easily add a Unicode or a UTF-8 BOM to a BLOB file using the BlobAddBOM function.
Adding a Unicode or a UTF-8 BOM to a BLOB file using the BlobAddBOM function:
-
For the purpose of this example, begin by creating a sample BLOB file. Save it under your <Magic xpi installation>\projects\<current project> folder. Call this file utf8.txt.
-
Now, create the variables that you will use in this example. Open the Variable repository and, under the Flow Variable tab, create a BLOB variable called F.utf8nobom and a logical variable called F.logic.
-
Drag a Flow Data Service into your flow.
-
To begin the process of adding a BOM, you need to convert your chosen file to a BLOB. In the Flow Data Configuration dialog box, click New.
-
In the Action column, select Update from the drop-down list.
-
In the Type column, select Flow.
-
In the Name column, select the F.utf8nobom 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:
File2Blb (EnvVal ('currentprojectdir')&'utf8.txt')
-
The next stage is to add a BOM to the newly-created BLOB. This is done by using the BlobAddBOM function. In the Flow Data Configuration dialog box, click New.
-
Enter the same parameters that you entered in steps 5 to 8, above.
-
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.utf8nobom ,1)
Note:
|
To add a UTF-16 BOM, use the following expression: BlobAddBOM ( F.utf8nobom ,2).
|
|
-
Now you need to convert your BLOB with its newly-added BOM back to the original file format. You do this with the Blb2File function. In the Flow Data Configuration dialog box, click New.
-
In the Action column, select Update from the drop-down list.
-
In the Type column, select Flow.
-
In the Name column, select the F.logic 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.utf8nobom , EnvVal('currentprojectdir') &'utf8withbom.txt')
-
Click OK to close the Flow Data Service.
-
Build your project.
-
Now, when you debug and run your project, you will see a new .txt file in the <Magic xpi installation>\projects\<current project> folder called utf8withbom.txt. This file is your original BLOB file with its newly-added BOM. Its name is taken from the expression that you entered in step 20. You can use a Hex Editor to see the new BOM.
How Do I Remove a Unicode or a UTF-8 BOM from a BLOB File?