Breaking News

Friday, July 2, 2010

How to view arabic in Console apps?

How to view arabic in Console apps?

You cant;

The Console applications do not support Arabic Text for input and output due to a limitation in the operating system console support. MSDN

Reason might be because Console is not set to handle multi-byte character-sets or 16-bit Unicode.

So, how to view Arabic numbers in WinForms or WebForms?

It seems that generally Arabic numerals are used in English. Though, the Date controls are culture-aware, the numbers are not :)

This means you will have to convert the numerals to arabic manually, at least so it seems.

So, you can write an extension method to achieve that:

public static string ToArabicEx(this string str)
    if (!string.IsNullOrEmpty(str))
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("ar-AE");
        string[] nativeDigits = Thread.CurrentThread.CurrentCulture.NumberFormat.NativeDigits;

        return str.Replace('0', nativeDigits[0].ToCharArray()[0])
                .Replace('1', nativeDigits[1].ToCharArray()[0])
                .Replace('2', nativeDigits[2].ToCharArray()[0])
                .Replace('3', nativeDigits[3].ToCharArray()[0])
                .Replace('4', nativeDigits[4].ToCharArray()[0])
                .Replace('5', nativeDigits[5].ToCharArray()[0])
                .Replace('6', nativeDigits[6].ToCharArray()[0])
                .Replace('7', nativeDigits[7].ToCharArray()[0])
                .Replace('8', nativeDigits[8].ToCharArray()[0])
                .Replace('9', nativeDigits[9].ToCharArray()[0]);
    else return str;


for (int i = 0; i <= 9; i++)
    Response.Write(strLineBreak + i.ToString().ToArabicEx());
    Response.Write(strLineBreak + DateTime.Now.ToString().ToArabicEx());


Happy coding!

No comments:

Post a Comment

Designed By Published.. Blogger Templates