Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

c# - Converting Numbers from Western Arabic Digits "1,2,3..." to Eastern Arabic Digits "?, ?, ?...."

I need to convert date stored in database into Hijri and display the same in Arabic

I used the Culture to convert the date which it does but it still display date as English numbers

Example Gregorian Date = 19/01/2012 Its equivalent date in Hirji is 25/02/1433

Following code snippet converts but displays same as 25/02/1433 While i want it in Arabic numbers something like ??/??/????"

string sDate    
DateTime dtt = Convert.ToDateTime("19/01/2012");
CultureInfo ci = new CultureInfo("ar-SA");
sDdate = dtt.ToString("d", ci);

Is there a was it converts date to Hijri and display same as Arabic

I need this for a web project which i am developing in ASP.NET c#

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The CultureInfo class will not help you in either parsing or formatting the numbers to eastern Arabic ("?", "?", "?", "?", "?", "?", "?", "?", "?", "?") nor to western Arabic ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"). You have to manually convert it, Here is a little function which will do that for you in a neat way:

public string ConvertToEasternArabicNumerals(string input)
    {
        System.Text.UTF8Encoding utf8Encoder = new UTF8Encoding();
        System.Text.Decoder utf8Decoder = utf8Encoder.GetDecoder();
        System.Text.StringBuilder convertedChars = new System.Text.StringBuilder();
        char[] convertedChar = new char[1];
        byte[] bytes = new byte[] { 217, 160 };
        char[] inputCharArray = input.ToCharArray();
        foreach (char c in inputCharArray)
        {
            if (char.IsDigit(c))
            {
                bytes[1] = Convert.ToByte(160 + char.GetNumericValue(c));
                utf8Decoder.GetChars(bytes, 0, 2, convertedChar, 0);
                convertedChars.Append(convertedChar[0]);
            }
            else
            {
                convertedChars.Append(c);
            }
        }
        return convertedChars.ToString();
    }

Now alter your code a little to look like this:

string sDate    
DateTime dtt = Convert.ToDateTime("19/01/2012");
CultureInfo ci = new CultureInfo("ar-SA");
sDate = ConvertToEasternArabicNumerals(dtt.ToString("dd/MM/yyyy", ci));

And things will work just fine. BTW, the code for the function was taken from here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...