0

I have a large group of Hindi numbers which i want to convert into numeric values but i don't know how to convert them . Please suggest me appropriate way to achieve this. Note Please don't suggest me replace method.

eg. convert this number २०७४ to equivalent to 2074.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 1
    *Please don't suggest me replace method.* So what do you expect then? If we don't replace them, how would we convert them? – Patrick Hofman Jul 31 '18 at 12:12
  • 2
    Have you tried setting the Hindi culture, then `int.Parse` ? – Neil Jul 31 '18 at 12:13
  • Actually i don't know in advance what the number would be, so i want general solution to achieve this. could you please explain how would you do this? – Rizwan Ansari Jul 31 '18 at 12:16
  • Are those characters just different glyphs for 0-9? Meaning, *could* you do it with a simple replacement? Or is it a different numeric system altogether? – Lasse V. Karlsen Jul 31 '18 at 12:16
  • It is unclear what you want to achieve right now. Can you show what you have got already? – Patrick Hofman Jul 31 '18 at 12:16
  • I think the Replace method would be simpler and fastest solution and i'm not sure what issue you are facing using replace method. You just have to create a list with 10 items (0 to 9). – SH7 Jul 31 '18 at 12:18
  • 1
    You need to tell us why you can't make do with "the replace method" as it seems this would be pretty straightforward to do. According to [the wikipedia page](https://hi.wikipedia.org/wiki/%E0%A5%AA) which shows a table for the numbers it seems they're just different symbols for `0-9`. – Lasse V. Karlsen Jul 31 '18 at 12:21

1 Answers1

2

I believe this is what you're after but be aware that this code is written by someone who doesn't speak Hindi, read Hindi or know Hindi.

I found the digits on the wikipedia page but I absolutely have no idea what I'm doing.

The google page (which I found by just googling for the individual digits from the original string in the question) seems to indicate the following:

  • The digits for 0-9 are ०१२३४५६७८९
    • I clicked on a link and used the last character of the url as the digit
    • Note that 4 had to be gotten as the second digit of 14, and there seems to be a disambiguity suffix on that link as well
  • They have unicode code points ranging from 2406 through 2415, in that order
  • The double digits numbers follow the system to a tee, so it seems to be just a 10-digit numeric system using different code points
    • But note that there are far too few examples for me to be absolutely certain this holds true for all numbers

If anyone pokes hole in this answer I will take it down but feel free to grab all the code from it first if you think it can be improved.

Also bear in mind that the OP explicitly asked for a non-replace method. The whole thing can probably be written in a oneliner with that but since that doesn't seem to be an acceptable answer then here we are.

With all that said, here's a non-string-replace version that mimicks basic numeric parsing using different symbols:

Note: There's about 7 tons of error-handling that isn't present here, such as empty strings, etc.

public static bool TryParseHindiToInt32(string text, out int value)
{
    const int codePointForZero = 2406;
    const int codePointForNine = codePointForZero + 9;

    int sign = +1;

    int index = 0;
    if (index < text.Length && text[index] == '-') // todo: hindi minus?
    {
        index++;
        sign = -1;
    }

    value = 0;
    while (index < text.Length)
    {
        char c = text[index];
        if (c < codePointForZero || c > codePointForNine)
        {
            value = 0;
            return false;
        }

        if ((uint)value > 214748364u)
        {
            value = 0;
            return false;
        }

        value *= 10;
        value += (c - codePointForZero);
        index++;
    }

    value *= sign;
    return true;
}

Test:

string digits = "२०७४";
TryParseHindiToInt32(digits, out int i);
Console.WriteLine(i);

Outputs:

2074
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825