hamitay
hamitay

Reputation: 53

convert Arabic numerical to English

i am looking for a way to convert the Arabic numerical string "٠١٢٣٤٥٦٧٨٩" to an English numerical string "0123456789"

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       dim Anum as string ="٠١٢٣٤٥٦٧٨٩"
       dim Enum as string =get_egnlishNum(Anum)
End Sub

private function get_egnlishNum(byval _Anum as string) as string

''   converting code

end function

Upvotes: 4

Views: 2983

Answers (4)

Andrew Morton
Andrew Morton

Reputation: 25057

You can simply replace the Arabic characters with the Western versions:

Dim arabicDigits = "٠١٢٣٤٥٦٧٨٩".ToCharArray
Dim s = "‏٠٦٦٢٧٣٩٦عدد النقاط هي ٩٩٣٩٣٥"

For i = 0 To arabicDigits.Length - 1
    s = s.Replace(arabicDigits(i), i.ToString)
Next

s now contains "‏06627396عدد النقاط هي 993935"

Upvotes: 1

Zeinab Hashem
Zeinab Hashem

Reputation: 21

Dim arabicDigits = "٠١٢٣٤٥٦٧٨٩".ToCharArray
      dim i as integer=0
For i = 0 To anum.Length - 1
select case arabicDigits(i)
case "٠" 
Replace(arabicDigits(i),"0")
case "١"
Replace(arabicDigits(i),"1")
case "٢"
Replace(arabicDigits(i),"2")
case "٣"
Replace(arabicDigits(i),"3")
case "٤"
Replace(arabicDigits(i),"4")
case "٥"
Replace(arabicDigits(i),"5")
case "٦"
Replace(arabicDigits(i),"6")
case "٧"
Replace(arabicDigits(i),"7")
case "٨"
Replace(arabicDigits(i),"8")
case "٩"
Replace(arabicDigits(i),"9")
i=i+1

endselect
End Sub

Upvotes: 0

Oybek
Oybek

Reputation: 7243

This is one of the solutions.

Function Convert(ByVal input As String) As String
    Dim source = "٠١٢٣٤٥٦٧٨٩"
    Dim target = "0123456789"
    Dim sb As New StringBuilder()
    For Each el in Input
        sb.Append(target(source.IndexOf(el)))
    Next
    Return sb.ToString
End Function

EDIT

I've tried to find out more "native" ways. What I found is the NativeDigits property of NumberFormatInfo class

This was my test code but it didn't succeed. But it can be a good starting point.

        Dim source = "١٢٣٤٥٦٧٨٩"
        Dim result As Integer
        Dim numInfo As new NumberFormatInfo()
        numInfo.NativeDigits = New String() { "٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩" }
        Int32.TryParse(source, NumberStyles.Any, numInfo, result)

Upvotes: 2

Saeb Amini
Saeb Amini

Reputation: 24439

You're looking for the GetNumericValue method of the char type which converts any numeric Unicode character to a double. For example:

double two = char.GetNumericValue('٢');
Console.WriteLine(two); // prints 2

For your example:

static string ArabicToWestern(string input)
{
    StringBuilder western = new StringBuilder();
    foreach(char num in input)
    {
        western.Append(char.GetNumericValue(num));
    }
    return western.ToString();
}

Modify per your needs.

VB.NET:

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

Upvotes: 4

Related Questions