Saturday, 17 November 2012

Convert English numbers into Arabic charchters

This artical is how to convert the English like 123456789 to ١٢٣٤٥٦٧٨٩٩٠


 public string ConvertToEasternArabicNumerals(string input)
        {
            System.Text.UTF8Encoding utf8Encoder = new System.Text.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();
        }

5 comments:

  1. How to get back from arabic character into English number ?

    ReplyDelete
    Replies
    1. c# code
      static string ArabicToWestern(string input)
      {
      StringBuilder western = new StringBuilder();
      foreach(char num in input)
      {
      western.Append(char.GetNumericValue(num));
      }
      return western.ToString();
      }

      Delete
  2. try this code
    Private Shared Function ArabicToWestern(ByVal input As String) As String
    Dim western As StringBuilder = New StringBuilder
    For Each num As Char In input
    western.Append(Char.GetNumericValue(num))
    Next
    Return western.ToString
    End Function

    ReplyDelete
    Replies
    1. thank you for anser my question
      i have try your code,
      but the result like this :
      ٩/١٦/٢٠١٣

      9-116-12013

      Delete
    2. with this code i can

      Public Function balikeun(ByVal input As String) As String
      Dim western As StringBuilder = New StringBuilder
      For Each num As Char In input
      ' If num = "/" Then Continue For
      If Not num = "/" Then western.Append(Char.GetNumericValue(num)) Else western.Append("/")
      Next
      Return western.ToString
      End Function

      Delete