encode()
The encode() function converts a string to binary using a specified character encoding and returns it as binary.
Syntax
encode(STR_EXPR[, CHARSET])
Parameters
STR_EXPR- A string or expression to convert to binary. If an array is passed, each element is converted individually.
CHARSET- The character encoding name. Defaults to
utf-8if omitted. Use the Preferred MIME Name or Aliases registered in the IANA Charset Registry. Refer to the IANA Charset Registry for the list of registered character encodings.
Description
The encode() function converts a string to binary (byte[]) using the specified character encoding.
Returns null if the input value is null or not a string. If an array or list is passed, the conversion is applied recursively to each element.
An error is raised at query parse time if an unsupported character encoding name is specified.
Error codes
| Error code | Description |
|---|---|
| 90811 | An unsupported character encoding was specified. |
Usage examples
-
Convert a string to UTF-8 binary
json "{'val': 'hello, world!'}" | eval bin = encode(val) | # bin: 68656c6c6f2c20776f726c6421 -
Convert with a specified character encoding
json "{'val': 'hello'}" | eval bin = encode(val, "euc-kr") -
nullinputjson "{'val': null}" | eval bin = encode(val) | # bin: null
Compatibility
The encode() function has been available since before Sonar 4.0.