qtg
qtg

Reputation: 125

Convert full-width digits `0` through `9` to corresponding normal `0` through `9` in C#

if (char.IsDigit(e.KeyChar))
{                        
    if (IsCharFullWidthDigits(e.KeyChar))
    {
        e.KeyChar = Strings.Chr(Strings.Asc(e.KeyChar) + 23680);
    }
}

If user inputs a full-width digit such as to , how to auto convert to the corresponding normal 0 or 9? In VB.NET I used Strings.Chr and Strings.Asc, but I don't see the equivalent in C#. How can I do this?

Upvotes: 0

Views: 182

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

Try this:

int diff = (int)'0' - (int)'0';
if (char.IsDigit(e.KeyChar) && IsCharFullWidthDigits(e.KeyChar))
{
        e.KeyChar = (char)(((int)e.KeyChar) - diff);
}

Upvotes: 2

Related Questions