Unicode – F # – card is not suitable for display in the console

I am using f# to make a console-based card game, and I am trying to use unicode chars to display card suits. The mapping suit-to-char represents the following function:

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

Show this use

printf "%s" <| suitSymbol Spades

It works fine in fsi:

fsi
But when When compiled with fsc.exe, it will display different (unsuitable) characters:

 cmd prompt

I have tried to change the encoding of the source file, but it has no effect. Is there a way to make it work at compile time?

Edit (30.01.2017):
Stuart's anwser is correct, but I can’t understand it, it needs to be entered

chcp 65001 

Every time I want to run my game.

After studying the method of referencing DLL in F#, I came up with the following solution:

module Kernel =
[]
extern bool SetConsoleOutputCP(uint32 wCodePageID)

In the main function code I added

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

It only modifies the code page of this process, so other applications will run normally.

In the command prompt, you need to change the code page as follows:

chcp 65001

After some tests, I can Reproduce your problem and solve it. Credit to @s952163

I am making a console-based card game with f#, and I am trying to use unicode chars Show card suits. Mapping suit-to-char represents the following function:

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

Show this usage

printf "%s" <| suitSymbol Spades

It works fine in fsi:

fsi
but When compiled with fsc.exe, it will display different (unsuitable) characters:

cmd prompt

I have tried to change the encoding of the source file, but it has no effect. Is there a way to make it work at compile time?

Edit (30.01.2017):
Stuart's anwser is correct, but I can’t understand it, it needs to be entered

chcp 65001 

Every time I want to run my game.

After studying the method of referencing DLL in F#, I came up with the following solution:

module Kernel =
[]
extern bool SetConsoleOutputCP(uint32 wCodePageID)

In the main function code I added

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

It only modifies the code page of this process, so other applications will run normally.

At the command prompt, you need to change the code page , As follows:

chcp 65001

After some tests, I was able to reproduce your problem and solve it. Credit to @s952163

Leave a Comment

Your email address will not be published.