You can use Encoding.UTF8 in OCL to convert text to UTF-8 bytes or UTF-8 bytes back to text when working with files and external integrations.
What UTF-8 is
UTF-8 (Unicode Transformation Format, 8-bit) is a variable-length character encoding for Unicode text. It can represent ordinary Latin text as well as characters such as á, é, ó, and 😊.
In MDriven OCL, UTF-8 is available through the standard .NET Encoding class. Use it when the value on one side of an integration is text and the value on the other side is a byte array.
Encode text as UTF-8 bytes
Use GetBytes(string) to create a byte array from a string.
Encoding.UTF8.GetBytes('Malmö 😊')
This expression returns the UTF-8 byte representation of Malmö 😊. Use the resulting byte array where an external system, file operation, or other integration expects bytes rather than a string.
Decode UTF-8 bytes as text
Use GetString(bytearray) to read a UTF-8 byte array as a string.
Encoding.UTF8.GetString(bytearray)
For example, if bytearray contains the UTF-8 bytes for Malmö 😊, the expression returns that text as a string.
Choose the encoding that matches the data
The encoding used to decode a byte array must match the encoding used to create it. If an external system specifies UTF-8, use Encoding.UTF8 for both directions.
| Task | OCL expression |
|---|---|
| Convert a string to UTF-8 bytes | Encoding.UTF8.GetBytes('Malmö 😊')
|
| Convert UTF-8 bytes to a string | Encoding.UTF8.GetString(bytearray)
|
UTF-8 and Base64
UTF-8 bytes and Base64 are different things. UTF-8 defines how text is represented as bytes. Base64 represents bytes using text characters, which can be useful when an integration accepts text only.
To convert a string to UTF-8 data stored in Base64 format, use StringToBase64. Do not treat a Base64 value as the original clear-text string; decode it when you need the underlying data.
