Skip to main content
Converts a VARCHAR string to a binary value based on the specified binary format (binary_type) of the input string. The following binary formats are supported: hex, encode64, and utf8. If no binary_type is specified, hex is the default.

Syntax

Parameters

  • str: the string to convert, required. It must be a VARCHAR string.
  • binary_type: the binary format for conversion, optional.
    • hex(default): to_binary assumes the input string is a hex string in which all characters are in ‘0123456789abcdef’. If the input string is not valid, an empty binary is returned (exceptions will not be thrown). to_binary will convert the input string into binary directly. For example, "abab" will be converted to x'abab'. The input string is not case-sensitive.
    • encode64: to_binary assumes the input string is a base64-encoded string . If the input string is not valid, an empty binary is returned (exceptions will not be thrown). to_binary will decode the base64-encoded string as the binary result. For example, "YWJhYg==" will be converted to x'abab'.
    • utf8: to_binary converts the input string as a binary value without any transformation.

Return value

Returns a VARCHAR value.

Examples

The following examples assume that the --binary-as-hex option is enabled when you access the database from your MySQL client. This way, binary data is displayed using hexadecimal notation.

References