Android Code Snippet Convert English numbers to Arabic or Persian

somed3v3loper

Well-Known Member
Licensed User
B4A/J translated from here https://github.com/ALI-KH-Y/AnyNum/blob/master/AnyNum/src/main/java/ir/ali_kh_y/anynum/AnyNum.java
with some code from Erel I forgot the post I copied from :D
B4X:
Public Sub enToAr(text As String) As String
    Private arabicChars() As String = Array As String("\u0660", "\u0661", "\u0662", "\u0663", "\u0664","\u0665", "\u0666", "\u0667", "\u0668", "\u0669")
    Dim builder As StringBuilder
    builder.Initialize
    For i =0 To text.Length-1
        If IsNumber(text.CharAt(i)) Then
            Dim ind As String =text.charAt( i)
            Dim indint As Int = ind
            builder.append(UnescapeUnicode(arabicChars(indint)))
        Else
            builder.Append(text.charAt( i))
        End If
    Next
    Return builder.ToString
End Sub

Public Sub enToFa(text As String) As String
    Private persianChars() As String = Array As String("\u06f0", "\u06f1", "\u06f2", "\u0663", "\u06f4", "\u06f5", "\u06f6", "\u06f7", "\u06f8", "\u0669")
    Dim builder As StringBuilder
    builder.Initialize
    For i =0 To text.Length-1
        If IsNumber(text.CharAt(i)) Then
            Dim ind As String =text.charAt( i)
            Dim indint As Int = ind
            builder.append(UnescapeUnicode(persianChars(indint)))
        Else
            builder.Append(text.charAt( i))
        End If
    Next
    Return builder.ToString
End Sub
public Sub UnescapeUnicode(s As String) As String
    Dim sb As StringBuilder
    sb.Initialize
    Dim i As Int
    Do While i < s.Length
        Dim c As Char = s.CharAt(i)
        If c = "\" And i < s.Length - 1 And s.CharAt(i + 1) = "u" Then
            Dim unicode As StringBuilder
            unicode.Initialize
            i = i + 2
            Do While i < s.Length
                Dim cc As String = s.CharAt(i)
                Dim n As Int = Asc(cc.ToLowerCase)
                If (n >= Asc("0") And n <= Asc("9")) Or (n >= Asc("a") And n <= Asc("f")) Then
                    unicode.Append(s.CharAt(i))
                Else
                    i = i - 1
                    Exit
                End If
                i = i + 1
            Loop
            sb.Append(Chr(Bit.ParseInt(unicode.ToString, 16)))
        Else
            sb.Append(c)
        End If
        i = i + 1
    Loop
    Return sb.ToString
End Sub
 
Top