Displaying Arabic characters in C# console application
There are several issues to resolve to get this to work.
- You need a font that supports both Arabic AND the windows console.
See KB : Necessary criteria for fonts to be available in a command window
The font must be a fixed-pitch font.
The font cannot be an italic font.
The font cannot have a negative A or C space.
If it is a TrueType font, it must be FF_MODERN.
If it is not a TrueType font, it must be OEM_CHARSET.
- You must install the font.
For testing, I used DejaVu Mono, which is one of the few that supports Arabic. Arabic is a tough language to make a monotype font with since the aesthetics of the language do not work well with a fixed width for each character. Nevertheless, this font makes an honest effort. For other possible alternatives, see :
complete, monospaced Unicode font?
The font must be installed in the normal way for your version of Windows (in Vista/7/8 this is right-click, Install
on the .ttf file). Once this is done, you have to follow the directions in the KB.
- Registry Editor --> HKLM\Software\Microsoft\WindowsNT\CurrentVersion\Console\TrueTypeFont
- Add a new string value named "
000
" with the valueDejaVu Sans Mono
- Reboot
Once you've rebooted, you can change the font in the console by selecting "Properties" from the console menu and changing the font in the "Font" tab.
Result.
... so after all that, we discover that the console does not support Right-To-Left languages. I guess you could use a function like :
static string Reverse(string text)
{
if (text == null) return null;
char[] array = text.ToCharArray();
Array.Reverse(array);
return new String(array);
}
and then do
Console.OutputEncoding = System.Text.Encoding.Unicode;
Console.WriteLine(Reverse("مرحبا بك"));
Set C# console application to Unicode output
It turns out that there are multiple things you need to set up in order to make the console display Unicode characters.
- Set the console to a Unicode supported font. To do this, run your C# console application once with
Console.ReadKey();
so the window stays open. Right click on the title bar of the window and select Properties. These options will persist when debugging through Visual Studio. You might need to use the Default menu instead for persisting the options throughout the system. In the Fonts tab, you need to set the font toLucida Console
. This font supports Unicode characters. The related post can be found here. Set the console's code page to UTF-8. This one is a bit tricky. Because, you have to execute a command in the console window to change the code page. For whatever reason, this option is not available as a console preference. To do this, you'll need to make a separate
cmd.exe
process, and use this instead of the normal console provided.var cmd = new Process
{
StartInfo =
{
FileName = "cmd.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
UseShellExecute = false
}
};
cmd.Start();
cmd.StandardInput.WriteLine("chcp 65001");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();The first part of the code above will create a new
cmd.exe
process. The settings given to StartInfo will make sure thatConsole
is redirected to this new process. The second part of the code sends a command to this console window and runs it. That command,chcp 65001
, sets the console's code page to UTF-8. Related posts can be found here and here.Set the OutputEncoding to UTF-8. This is the only way that
Console.WriteLine
will actually output Unicode characters. Setting this is very simple.Console.OutputEncoding = Encoding.UTF8;
Now, any output from
Console
will be in Unicode. The related post can be found here.
So, that's it! I hope this information helps someone. :-)
Arabic characters issue
Thanks for all answers. The problem was not with iPhone side. In server the XML was not correctly encoded to UTF8 string. When it correctly encoded I got valid arabic character. I only did code like
NSString* lableText = [[NSString alloc] initWithData:serverSentData encoding: NSUTF8StringEncoding ];
for converting NSData to NSString and it worked well!!!
How read form file Text language Arabic
Your code reads the text correctly into the variable text
. (Debug and See)
However, dispalying arabic characters in the windows Console is another issue (Check how to solve it Here)
How to write Unicode characters to the console?
It's likely that your output encoding is set to ASCII. Try using this before sending output:
Console.OutputEncoding = System.Text.Encoding.UTF8;
(MSDN link to supporting documentation.)
And here's a little console test app you may find handy:
C#
using System;
using System.Text;
public static class ConsoleOutputTest {
public static void Main() {
Console.OutputEncoding = System.Text.Encoding.UTF8;
for (var i = 0; i <= 1000; i++) {
Console.Write(Strings.ChrW(i));
if (i % 50 == 0) { // break every 50 chars
Console.WriteLine();
}
}
Console.ReadKey();
}
}
VB.NET
imports Microsoft.VisualBasic
imports System
public module ConsoleOutputTest
Sub Main()
Console.OutputEncoding = System.Text.Encoding.UTF8
dim i as integer
for i = 0 to 1000
Console.Write(ChrW(i))
if i mod 50 = 0 'break every 50 chars
Console.WriteLine()
end if
next
Console.ReadKey()
End Sub
end module
It's also possible that your choice of Console font does not support that particular character. Click on the Windows Tool-bar Menu (icon like C:.) and select Properties -> Font. Try some other fonts to see if they display your character properly:
How to read Arabic characters from HttpWebResponse in C#
Thanks @VahidN who gave answer in comment, I just want to write it here so it will be easier for everyone to see it.
Answer:
Console.WriteLine is not able to show Unicode characters. save it to a file and the examine it
That worked.
Thanks!
Related Topics
How to Get Current Page Url in MVC 3
Why Does Resharper Want to Use 'Var' for Everything
Mobile Device Detection in ASP.NET
How to Round to the Nearest Whole Number in C#
How to Find the "Clamp" Function in .Net
Windows.Forms.Timer or System.Threading.Timer
How to Return Anonymous Type from C# Method That Uses Linq to SQL
Is There a Lower Bound Function on a Sortedlist<K ,V>
How to View Msil/Cil Generated by C# Compiler? Why Is It Called Assembly
How to Convert List<String> to List<Int>
Client Side Groupby Is Not Supported
Detect Active Window Changed Using C# Without Polling
Ef Code First: How to Get Random Rows
"Updatesourcetrigger=Propertychanged" Equivalent for a Windows Phone 7 Textbox
How to Treat the Circle as a Control After Drawing It? - Moving and Selecting Shapes