Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# - card suits not displaying in console

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.

like image 357
Muchtrix Avatar asked Jan 22 '17 22:01

Muchtrix


1 Answers

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

like image 163
Stuart Avatar answered Sep 28 '22 03:09

Stuart