If you do not need any dependencies outside the standard library shipped with OCaml *, you can also convert the character to a string using the format string.
Create a 1 digit string from the character
Printf.sprintf "%c" ch
(note that capital C ) Create a string containing the OCaml notation for this character
Printf.sprintf "%c" ch
eg:
# Printf.sprintf "%C" '\\';; - : string = "'\\\\'" # Printf.sprintf "%C" 'a';; - : string = "'a'"
* Printf not part of Pervasives
Gregory nisbet
source share