class conversion_pipe :
?subst:int -> string -> in_enc:encoding -> out_enc:encoding -> unit ->
Netchannelsfor more information) can be used to recode a netchannel while reading or writing. The argument
in_encis the input encoding, and
out_encis the output encoding.
The channel must consist of a whole number of characters. If it
ends with an incomplete multi-byte character, however, this is
detected, and the exception
Malformed_code will be raised.
This exception is also raised for other encoding errors in the
Example. Convert ISO-8859-1 to UTF-8 while writing to the file
let ch = new output_channel (open_out "output.txt") in let encoder = new conversion_pipe ~in_enc:`Enc_iso88591 ~out_enc:`Enc_utf8 () in let ch' = new output_filter encoder ch in ... (* write to ch' *) ch' # close_out(); ch # close_out(); (* you must close both channels! *)
If you write as UTF-16, don't forget to output the byte order mark yourself, as the channel does not do this.
Example. Convert UTF-16 to UTF-8 while reading from the file
let ch = new input_channel (open_in "input.txt") in let encoder = new conversion_pipe ~in_enc:`Enc_utf16 ~out_enc:`Enc_utf8 () in let ch' = new input_filter ch encoder in ... (* read from ch' *) ch' # close_in(); ch # close_in(); (* you must close both channels! *)
subst: This function is invoked for code points of
in_encthat cannot be represented in
out_enc, and the result of the function invocation is substituted (directly, without any further conversion). Restriction: The string returned by
substmust not be longer than 50 bytes. If
Cannot_representis raised in this case.