4

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?

JonnyBoats
  • 5,177
  • 1
  • 36
  • 60
  • have you set the [`Console.OutputEncoding`](http://msdn.microsoft.com/en-us/library/system.console.outputencoding.aspx)? and are you using a console font that supports those characters? – Mgetz Jul 07 '13 at 17:08
  • Interestingly if one runs this code at http://www.tryfsharp.org it produces the correct output. – JonnyBoats Jul 08 '13 at 10:48

1 Answers1

9

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:

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.

Community
  • 1
  • 1
Jack P.
  • 11,487
  • 1
  • 29
  • 34
  • you also need to have a font set on the console that supports unicode, the default raster font does not – Mgetz Jul 07 '13 at 17:16
  • @Mgetz Yep, I was just editing my answer to add that. The answer to the question I linked is very good and has all of the details needed to solve this problem. – Jack P. Jul 07 '13 at 17:16
  • So what font should I use and where should I get it? I am running Windows 8. – JonnyBoats Jul 07 '13 at 17:29
  • @JonnyBoats I can't recommend a specific one. I added some informational links to my answer for you -- it looks like you main option is adding a registry entry to allow some existing font on your machine to be used in the console, and failing that, you should try to find an TrueType font with Arabic support which meets the criteria listed in the KB article I linked. – Jack P. Jul 07 '13 at 19:43
  • Jack P: I would accept your answer except for the fact that it does not solve the question as asked. Specifically telling me to get a font that displays Arabic characters without telling me where to get it does not answer the question. – JonnyBoats Jul 08 '13 at 10:43
  • @JonnyBoats See my update -- it looks like you can use the `Courier New` font that ships with Windows. You just need to add a registry value so you can select it as a console font (or simply set it as the default). – Jack P. Jul 08 '13 at 12:15
  • Jack P - Bingo, you nailed it. Two quirks though, 1)the biggest point size I could get to work with Courier New is 8 point. Bigger than that and I get boxes instead of the proper Arabic characters. 2) The text is reversed. Arabic is a language written right to left but this displays the first character on the left. – JonnyBoats Jul 08 '13 at 20:32
  • "However, console applications, which employ the text user interface of the operating system console, do not provide bidirectional support." http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.getconsolefallbackuiculture%28v=vs.110%29.aspx – Bent Tranberg Sep 14 '14 at 08:56