8

I'm making console based card game in f# and I'm struggling with displaying card suits using unicode chars. Mapping suit-to-char is represented as following function:

let suitSymbol = function
    | Spades   -> "\u2660"
    | Clubs    -> "\u2663"
    | Diamonds -> "\u2666"
    | Hearts   -> "\u2665"

Displaying this using

printf "%s" <| suitSymbol Spades

works fine in fsi:

fsi
but when compiled using fsc.exe it displays diffrent (not suit) chars:

cmd prompt

I've tried changing encoding of source file but to no effect. Is there any way for it to work when compiled?

EDIT (30.01.2017): Stuart's anwser was correct, but I couldn't get over fact, that It required to enter

chcp 65001

every time I wanted to run my game.

After studying ways of referencing DLLs within F#, I came up with following solution:

module Kernel =
    [<DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)>]
    extern bool SetConsoleOutputCP(uint32 wCodePageID)

And in main function code I've added

[<EntryPoint>]
let main args =
    Kernel.SetConsoleOutputCP 65001u |> ignore

It modifies code page for this process only, so other apps will behave normally.

Muchtrix
  • 83
  • 4
  • I suspect this is to do with different fonts being used (even though they look very similar). – Stuart Jan 22 '17 at 23:19
  • After you compile it with fsc, where are you running it, and what are your font settings? – Stuart Jan 22 '17 at 23:19
  • It's the same font. Both screenshots are from the same console window. It's consolas run from cmd. – Muchtrix Jan 22 '17 at 23:21
  • 4
    @Muchtrix Can you check the code page of the command prompt? Try setting it to unicode, e.g. `chcp 65001` or something similar. – s952163 Jan 22 '17 at 23:25
  • Changing code page to 65001 works but still I find it curious that fsi displays it correctly even on my default code page which is 852 – Muchtrix Jan 22 '17 at 23:38
  • It will probably be to do with how they are launched. I was running everything directly from command prompt, fsi.exe didn't change the code page automatically. – Stuart Jan 22 '17 at 23:42

1 Answers1

4

In your command prompt you will need to change your code page like this:

chcp 65001

After some testing I was able to reproduce your issue, and this fixes it. Credit to @s952163

Stuart
  • 5,358
  • 19
  • 28