Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printfn not producing expected results for international (non-latin) characters

Tags:

f#

I have the following program:

let txt = "إتصالات"
printfn "Text is: %s" txt
0 // return an integer exit code

The value of txt is being set to some Arabic characters. When I run the program what is being displayed on the console is a bunch of question marks rather than the characters. In the Visual Studio 2012 debugger the correct characters are being displayed for the txt variable.

What am I doing wrong and how does one properly display international characters?

like image 384
JonnyBoats Avatar asked Jan 12 '23 23:01

JonnyBoats


1 Answers

According to How to write unicode chars to console? you need to set the OutputEncoding property on the console, like this:

System.Console.OutputEncoding <- System.Text.Encoding.Unicode
let txt = "إتصالات"
printfn "Text is: %s" txt
0 // return an integer exit code

The answer for that question is worth reading though, because it also describes why you need to change your console font to really make this work, and also how to do it.

Here are some additional links with more information:

  • Necessary criteria for fonts to be available in a command window (this is for Windows 2000 and may not entirely apply to Windows 8, but it should give you a good idea of what to look for in a font).
  • Windows Console and TrueType Fonts shows how to add new fonts to the console.
  • Anyone who says the console can't do Unicode isn't as smart as they think they are has some background information about writing Unicode text to the console.

Update: Since the Arabic text in the example renders just fine here on StackOverflow, I peeked at the CSS to see which fonts they're using to render preformatted text. Using that list and the Windows Character Map tool (Start -> All Programs -> Accessories -> System Tools -> Character Map), I've found the Courier New font (which ships with Windows) supports Arabic characters. If you use the registry hack in the "Windows Console and TrueType Fonts" link (above), you should be able to add Courier New as a font you can use in the console.

like image 92
Jack P. Avatar answered Feb 02 '23 21:02

Jack P.