You can use GetBytes in OCL to convert text into a byte array when an integration, storage format, or other operation requires binary data.
Syntax
Encoding.<EncodingName>.GetBytes(<StringExpression>)
GetBytes is a method on the standard .NET Encoding class. It takes a string expression and returns the bytes produced by the selected character encoding.
Convert a string to bytes
Choose an encoding, then call GetBytes with the string you want to convert.
Encoding.ASCII.GetBytes('Hello')
This expression converts Hello to an ASCII byte array.
You can also pass a string-valued expression:
Encoding.UTF8.GetBytes(self.Name)
In this example, the value of self.Name is converted to bytes using UTF-8.
Choose the encoding deliberately
The encoding determines how characters are represented as bytes. Use the encoding required by the system that will receive or later decode the data. If the encoding differs between conversion and decoding, characters may not be interpreted as intended.
| Encoding expression | Example use |
|---|---|
Encoding.ASCII
|
Convert text that is expected to use ASCII characters. |
Encoding.UTF8
|
Convert text using UTF-8. |
Encoding.Default
|
Decode or encode using the default encoding where that is required by the data or environment. |
For example, use the same encoding for both directions when you control the conversion:
Encoding.UTF8.GetBytes('MDriven')
The corresponding byte array can be converted back to text with Encoding.UTF8.GetString(bytearray). See Documentation:Encoding for GetString, available encodings, and guidance for decoding byte arrays.
When to use GetBytes
Use GetBytes when an operation needs bytes rather than a string. Typical cases include preparing textual data for binary storage or transmission. Keep the byte array as bytes until the receiving operation has consumed it; do not treat the result as a character collection such as the result of ToCharArray.
