[racket] current-output encoding

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun Mar 31 18:31:37 EDT 2013

At Tue, 26 Mar 2013 18:29:00 +0100, Pierpaolo Bernardi wrote:
> On Tue, Mar 26, 2013 at 5:25 PM, Pierpaolo Bernardi <olopierpa at gmail.com> wrote:
> > On Tue, Mar 26, 2013 at 5:00 PM, Matthew Flatt <mflatt at cs.utah.edu> wrote:
> >
> >> I think you want `reencode-output-port', probably using "" (which
> >> represents the user's default encoding) as the target encoding.
> >
> > I tried the following:
> >
> > ====
> > #lang racket
> >
> > (current-output-port (reencode-output-port (current-output-port) ""))
> >
> > (display "à")
> > ====
> >
> > but this displays nothing both in DrRacket and in the console.
> > Also "program >tempfile" from the console produces a file of length 0.
> 
> Adding a (flush-output) after the display makes it work from inside DrRacket.
> 
> In the console there's no change, and redirecting the output to a file
> still produces a 0-length file, though.

Ok, while `reencode-output-port' might be the right direction, there
are several complications:

 * On Windows, Racket uses UTF-8 for "". So, reencoding to "" doesn't
   do anything.

   That's the right choice for DrRacket, through, since DrRacket's
   interaction window uses UTF-8.

 * In a Windows console, the right encoding depends on how the console
   is configured. Use `chcp' to find out your code page. It will be a
   number N, and then the locale you want is "Windows-N".

   My default code page is 1252, so

     #lang racket

     (current-output-port (reencode-output-port (current-output-port)
                                                "windows-1252"))

     (display "à")
     (flush-output)

   works for me...

 * ... if I change the console font to a Unicode-capable one like
   "Consolas" or "Lucida Console".

 * The `(flush-output)' is needed to make sure the output is flushed
   on exit. Racket auto-flushes low-level ports on exit, but that doesn't
   help for a re-encoding port.

You could also set your console to UTF-8 with `chcp 65001', and then
the console and DrRacket will work the same (as long as you also pick a
suitable font in the console).

See also 
http://stackoverflow.com/questions/1259084/what-encoding-code-page-is-cmd-exe-using



Posted on the users mailing list.