4

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
casperOne
  • 73,706
  • 19
  • 184
  • 253
hamitay
  • 53
  • 2
  • 7
  • You can use just `String.Replace` method to do so. The only difference here is the representation. Underlying structure of numbers are the same. (Numbers in Arabic are written from left to right) – Oybek Mar 20 '12 at 11:22
  • 1
    Do you need to preserve leading `0`s? i.e. can you parse it as an integer, and then turn it back into a string again with a different culture? – CodesInChaos Mar 20 '12 at 11:32
  • There is a built in method for chars which works for all numeric representations, including Arabic numbers. See [my answer](http://stackoverflow.com/a/9786149/68080). – Saeb Amini Mar 20 '12 at 11:55
  • looks like there is nothing built in present in .NET for this but you can look at this project ... might helps. http://www.codeproject.com/Articles/7384/Convert-Arabic-Number-to-equivalent-Arabic-text – Rahul Mar 20 '12 at 11:24
  • hi thank you rahul, the requirement is as follows. i need when i receive a value in my string varible, i convert Arabic numbers to english numbers and keep everything else as it is for example ‏٠٦٦٢٧٣٩٦عدد النقاط هي ٩٩٣٩٣٥ after conversion ‏06627396عدد النقاط هي 993935 – hamitay Mar 20 '12 at 12:57
  • The simplest way is just to have it like `_Anum[i] -= '٠' + '0';` for each valid index `i` in your array with arabic numerals `_Anum` or whatever you decide to call it. – Stefan Marinov Mar 20 '12 at 11:20
  • my Arabic num will be referent each time , user is type the arabic num and i have to convert it to english – hamitay Mar 20 '12 at 11:22
  • You just will have to do it symbol by symbol. There is probably something already written for this purpose but I don't see how this can be avoided. – Stefan Marinov Mar 20 '12 at 11:24

4 Answers4

4

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
Saeb Amini
  • 23,054
  • 9
  • 78
  • 76
  • dear saeb, this is good but the thing is that i may have mixed string and numerical value. for example ‏ ‏٠٦٦٢٧٣٩٦عدد النقاط هي ٩٩٣٩٣٥٩ – hamitay Mar 20 '12 at 12:42
  • 2
    You could use the same fonctione but if GetNumericValue returns -1 you append num. – the_lotus Mar 20 '12 at 14:45
  • this convert . to - If Char.IsDigit(num) Then – Yusuf Sep 21 '22 at 03:59
2

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)
Oybek
  • 7,016
  • 5
  • 29
  • 49
  • can't i just play with the culture or encoding to do it automatically ? – hamitay Mar 20 '12 at 11:30
  • 1
    Perhaps you should add some better error handling for cases where the input contains characters not in `source`. Either by throwing a meaningful exception, or by simply preserving them. – CodesInChaos Mar 20 '12 at 11:31
  • 1
    @CodeInChaos Yes agree. I omitted the exception handling for simplicity. It assumes that input is already validated number-only string – Oybek Mar 20 '12 at 11:50
1

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"

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
0
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
  • That code is wrong on many levels, including the `Replace` that does nothing, manually incrementing `i` inside the loop and ignoring the input text completely. – GSerg May 16 '17 at 10:01