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.
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:
०१२३४५६७८९
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